Re: rand() and srand() broken?
Re: rand() and srand() broken?
- Subject: Re: rand() and srand() broken?
- From: Clark Cox <email@hidden>
- Date: Wed, 25 Nov 2009 14:41:53 -0500
On Wed, Nov 25, 2009 at 1:40 PM, 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?
Yes. If you pass the same value to seed(), you will get the same
sequence of values from rand(). Period. Any implementation of
srand/rand that does not do this is broken. From the C Standard:
"The srand function uses the argument as a seed for a new sequence of
pseudo-random numbers to be returned by subsequent calls to rand. If
srand is then called with the same seed value, the sequence of
pseudo-random numbers shall be repeated."
--
Clark S. Cox III
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