[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

problems with standard rand functions

mwbuda

1/22/2006 2:13:00 AM

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?

3 Answers

Timothy Goddard

1/22/2006 10:38:00 AM

0

# Start Script

total = Hash.new(0)

1000000.times do
total[(rand * 10).to_i] += 1
end

10.times do |t|
puts "#{t}0% - #{t + 1}0%: #{total[t] / 10000}%"
end

# End Script


# Start Output

00% - 10%: 10.0151%
10% - 20%: 10.009%
20% - 30%: 10.0326%
30% - 40%: 9.9543%
40% - 50%: 10.0381%
50% - 60%: 9.9985%
60% - 70%: 9.963%
70% - 80%: 9.97%
80% - 90%: 9.997%
90% - 100%: 10.0224%

# End Output

As you can see, there does not appear to be any simple skew at work
here. If you post your code, we may be able to identify the source of
any bias.

mwbuda

1/22/2006 5:04:00 PM

0


Timothy Goddard wrote:
> # Start Script
>
> total = Hash.new(0)
>
> 1000000.times do
> total[(rand * 10).to_i] += 1
> end
>
> 10.times do |t|
> puts "#{t}0% - #{t + 1}0%: #{total[t] / 10000}%"
> end
>
> # End Script
>
>
> # Start Output
>
> 00% - 10%: 10.0151%
> 10% - 20%: 10.009%
> 20% - 30%: 10.0326%
> 30% - 40%: 9.9543%
> 40% - 50%: 10.0381%
> 50% - 60%: 9.9985%
> 60% - 70%: 9.963%
> 70% - 80%: 9.97%
> 80% - 90%: 9.997%
> 90% - 100%: 10.0224%
>
> # End Output
>
> As you can see, there does not appear to be any simple skew at work
> here. If you post your code, we may be able to identify the source of
> any bias.

whoops, im an idiot! i forgot to clear out the array the generated
numbers were stuck in before perfroming a sort function, the result
being that high values from previous rolls werent tossed out.

thanks anyway

Eero Saynatkari

1/24/2006 8:35:00 PM

0

Matthew Moss 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://eige... has some further info.


E


--
Posted via http://www.ruby-....