News:

Building a 3D Ray Tracer  By stevmjon

Main Menu

Rnd() not uniform

Started by u9, July 17, 2008, 04:12:44 PM

Previous topic - Next topic

u9

The rnd() function does not seem to be uniform. The first and last value in the range only have half the probability of appearing as the rest of the range. I know, the documentation does not actually specify whether the function is uniform or not, but i believe it certainly is expected.

I have version 1.63v.

Can anyone confirm?


randomize timer()
dim arr(4)
for i = 0 to 1000000
num = rnd(3)
arr(num) = arr(num)+1
next

do
    cls 0
    print "0: " + str$(arr(0)) + "   1: " + str$(arr(1)) + "   2: " + str$(arr(2)) + "   3: " + str$(arr(3))
    sync
loop

SpellSword

#1
I think the RND command uses the value given by the Randomize command to run its random number generator, so the randomness of each number produced by a RND is an illusion of sorts.

For example:
Depending on what number is loaded into the Randomize Command, RND(3) could favor returning 3 50% of the time, or if a different number was loaded, it could return 3 only 10% of the time.

Think of the RND commands as calling the next number in a a huge sequence created by the number you placed into Randomize. Example:
1332313331332323133331333331333123333123233232332

In the example sequence of numbers, 3 is much more dominant than either 1 or 2.

At least, that's my understanding of it.

Edit: Typo
When I dream,
I carry a sword in one hand,
a gun in the other...

thaaks

Here is a very good article named "Never-ending Shuffled Sequences - When Random is too Random".
It should help you to easily code your own "weighted" random implementation in PB (although the source is in Java).
If you have any problems understanding the Java code just ask in this thread.

Hope that helps,
Tommy

kevin

#3
    RND() actually produces FLOATS,  and when you move them to an INT they're automatucally rounded. Which skews the results in this case.

    You can use the RND#() version and then Floor() the result to avoid the rounding



Dim arr(4)

For I = 0 to 1000000
num = int(rnd#(3))
arr(num) = arr(num)+1
next

For lp=0 to 3
  print str$(lp)+": "+str$(arr(lp))
next

 Sync
 Waitkey



 Read More -> Variables & Math Questions