[eu-gene] ansi randomness

Dave Griffiths dave at pawfal.org
Wed May 10 10:13:45 BST 2006


> On 9 May 2006, at 22:03, \jjs wrote:
>
>> but that MinGW implementation seems completely deterministic to me!
>> for instance- how can you get a number between 0 and
>> 6364136223846793006 [unless you can feed it a floating point number
>> between 0 and 1]?
>
> I'm guessing there's an overflow involved which will wrap the number
> round.

yup, that (I think) actually generates a 64bit value, the line I didn't
show takes the top significant portion of the number and packs it into a
32bit int, so you get a number from 0 to INT_MAX.

most calls to the ansi c rand() funtion are modulo'ed to the range you
want - ie rand()%100 will give you an int in the range 0-99. so float
randomness is usually implemented with something like (rand()%100)/100.0f
which will squash the range between 0 and 1.

the actual implementation is not really that important for most of our
uses for it - I'd like to know where the number 6364136223846793005 comes
from - some amalgamation of people birthdays is my guess.

fwiw, from said code:

/* This multiplier was obtained from Knuth, D.E., "The Art of
Computer Programming," Vol 2, Seminumerical Algorithms, Third
Edition, Addison-Wesley, 1998, p. 106 (line 26) & p. 108 */

>> but that MinGW implementation seems completely deterministic to me!

precisely! it's a machine :) every implementation is like that.

it's actually really important that it's deterministic - it can be
essential to get the same number sequence for a seed you enter. among
other things, I've been involved with rendering crowd sequences for film -
the randomisation of the agents in the crowd would be pretty hard to do if
you couldn't seed the random number generator deterministically, the crowd
would change every frame! ;)

cheers,

dave




More information about the eu-gene mailing list