Skip to content

Instantly share code, notes, and snippets.

@aldente-hu
Created December 7, 2020 02:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aldente-hu/5e913f4c9da5f80ae7fb7d3756a7e9b0 to your computer and use it in GitHub Desktop.
Save aldente-hu/5e913f4c9da5f80ae7fb7d3756a7e9b0 to your computer and use it in GitHub Desktop.
標準正規分布に従う乱数を取得する
class BoxMuller
def initialize(seed = Random.new_seed)
@prng = Random.new(seed)
@next = nil
end
def next
if @next
v = @next
@next = nil
v
else
r = Math::sqrt(-2 * Math::log(@prng.rand))
theta = 2 * Math::PI * @prng.rand
@next = r * Math::sin(theta)
r * Math::cos(theta)
end
end
end
# Usage:
# b = BoxMuller.new # You can give a random seed.
# b.next
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment