I have been working on some simple dice rollers, and the problem is
they tend to roll too high. for a given group of dice, say 3 6-sideds,
it tends to generate the higher numbers in the range more often than
the lower ones ( 4 to 5 rather than 1 to 3 ).
ive tried both reseeding for each group of dice ( to continue the
example above, all 3 6-sideds ) and reseeding for each dice ( i reseed
before generating each value b/w 1 and 6 )
any suggestions on how i can have a more even overall distribution of
numbers throughout the range?
hist = Hash.new(0)
1_000_000.times do |i|
hist[rand(6)] += 1
end
print hist.map { |k,v| “#{k}:#{v}\n” }.sort.join
My output:
0:166636
1:166867
2:166866
3:166923
4:166572
5:166136
Seems pretty uniform to me…
As I recall (and someone else might know better), Ruby uses the
Mersenne Twister which is pretty darn random. I haven’t seen the Ruby
source to know how they’re presenting it (ie, how they map to a range
of 0-5, for instance)
Matthew M. wrote:
hist = Hash.new(0)
1_000_000.times do |i|
hist[rand(6)] += 1
end
print hist.map { |k,v| “#{k}:#{v}\n” }.sort.join
My output:
0:166636
1:166867
2:166866
3:166923
4:166572
5:166136
Seems pretty uniform to me…
As I recall (and someone else might know better), Ruby uses the
Mersenne Twister which is pretty darn random. I haven’t seen the Ruby
source to know how they’re presenting it (ie, how they map to a range
of 0-5, for instance)
It is MT, indeed, but apparently the amount of bits actually
used from the randomization is limited which slightly reduces
the randomness. http://eigenclass.org has some further info.
E