Skip to content

Instantly share code, notes, and snippets.

@kimoto
Created June 6, 2012 04:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kimoto/2879854 to your computer and use it in GitHub Desktop.
Save kimoto/2879854 to your computer and use it in GitHub Desktop.
乱数の和が正規分布するやつのテストコード
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