Created
December 7, 2020 02:49
-
-
Save aldente-hu/5e913f4c9da5f80ae7fb7d3756a7e9b0 to your computer and use it in GitHub Desktop.
標準正規分布に従う乱数を取得する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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