Created
June 6, 2012 04:03
-
-
Save kimoto/2879854 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
def dice(max = 6) | |
(rand() * max).to_i + 1 | |
end | |
# 1. まずrubyの乱数(rand)の性質を調べる | |
puts "ruby rand() test" | |
d = {} | |
1000.times{ | |
n = dice | |
d[n] ||= 0 | |
d[n] += 1 | |
} | |
d.keys.sort.each{ |k| | |
puts "#{k} => #{d[k]}" | |
} | |
# 1の出力結果。一様乱数っぽい | |
# 1 => 166 | |
# 2 => 184 | |
# 3 => 164 | |
# 4 => 172 | |
# 5 => 153 | |
# 6 => 161 | |
# 2. この2つの乱数の和の性質を調べる | |
puts "rand() + rand() test" | |
d = {} | |
1000.times{ | |
n = dice + dice | |
d[n] ||= 0 | |
d[n] += 1 | |
} | |
d.keys.sort.each{ |k| | |
puts "#{k} => #{d[k]}" | |
} | |
# 2の出力結果。中央の値に集中してる | |
# 3 => 60 | |
# 4 => 92 | |
# 5 => 113 | |
# 6 => 147 | |
# 7 => 159 | |
# 8 => 143 | |
# 9 => 106 | |
# 10 => 76 | |
# 11 => 63 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment