# Forum: Ruby problems with standard rand functions

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
on 2006-01-24 17:51
```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?```
on 2006-01-24 19:29
```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)```
on 2006-01-24 22:35
```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```
This topic is locked and can not be replied to.