# Problems with standard rand functions

**URL:** <https://www.ruby-forum.com/t/problems-with-standard-rand-functions/53066>\
**Category:** Ruby\
**Created:** [January 24, 2006, 4:51pm UTC](https://www.ruby-forum.com/t/problems-with-standard-rand-functions/53066 "2006-01-24T16:51:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 24, 2006, 4:51pm UTC](https://www.ruby-forum.com/t/problems-with-standard-rand-functions/53066/1 "2006-01-24T16:51:37Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 24, 2006, 6:29pm UTC](https://www.ruby-forum.com/t/problems-with-standard-rand-functions/53066/2 "2006-01-24T18:29:46Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 24, 2006, 9:35pm UTC](https://www.ruby-forum.com/t/problems-with-standard-rand-functions/53066/3 "2006-01-24T21:35:16Z")

</div>

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](http://eigenclass.org) has some further info.

E
