Re: rand() and srand() broken?
Re: rand() and srand() broken?
- Subject: Re: rand() and srand() broken?
- From: "Daniel M. Zimmerman" <email@hidden>
- Date: Wed, 25 Nov 2009 13:43:22 -0500
--On 25 November 2009 11:40:10 -0700 Derek Gaston <email@hidden>
wrote:
On Nov 25, 2009, at 10:47 AM, Jason Foreman wrote:
On Nov 25, 2009, at 11:26 AM, Derek Gaston wrote:
This piece of code should generate 100 random numbers between 0 and
1... I am expecting that they be fairly spaced out as well...
What basis do you have for this expectation? Is it spelled out in
any specification or standard somewhere?
You're seeding the RNG with a monotonically increasing seed, and
getting what looks like monotonically increasing starting values for
your random sequences. The fact that you get what you expect on
Linux seems to be purely coincidental.
Are you (and everyone else that replied) trying to tell me that the first
number out of rand() (after reseeding) should always be the predictable?
Because that's essentially what I'm seeing. This, to me, feels very
wrong. If I change the seed.... I should see a different first number.
And you are... but the first number is, by definition, a function of the
seed, and apparently the function Apple is using is not to your liking.
That doesn't make it wrong; it just isn't the same non-guaranteed behavior
you were relying on.
(snip)
The issue is that we need to be able to
reliably _regenerate_ different random numbers over and over.... but on a
grand scale. Think of a matrix... you need to be able to get a random
number for each entry in the matrix (let's say it's 10000000x10000000...
so a LOT of numbers). BUT... you don't want to _store_ all of those
numbers.... you just want to be able to query what the random number for
any given entry is. So we come up with a way of guaranteeing unique seed
numbers (for instance, using combinations of row and column numbers for
this matrix example). Then we can always query a place in the matrix
just by reseeding and calling rand(). It will reliably always give us
back the same number over and over for a given seed.
Sure. But then you are highly dependent on the particular PRNG function
being used by rand() on that platform, which is of course not particularly
well specified.
Maybe to get the random number for a particular row/column, you could seed
the PRNG with your row/column number combination (however you calculate
that) and then discard the first K random numbers it generates, where K is
an easy-to-compute function of your row/column numbers. That would probably
give you the "randomness" you're looking for, and would probably not be too
much extra overhead.
-Dan
------------------------------------------------------------------
Daniel M. Zimmerman TFF Enterprises
1900 Commerce St. Box 358426 http://www.tffenterprises.com/~dmz/
Tacoma, WA 98402 USA email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden