Re: Seeding random() randomly
Re: Seeding random() randomly
- Subject: Re: Seeding random() randomly
- From: Ken Thomases <email@hidden>
- Date: Thu, 26 May 2011 20:18:49 -0500
On May 26, 2011, at 8:00 PM, Graham Cox wrote:
> I'm using random(), but every time I run my app I get the same sequence, despite having this code in my app delegate's -appDidFinishLaunching method. Clearly I'm not seeding it right, though I can't see why - I get a different value for seed every time. What gives?
>
>
> unsigned seed = (unsigned)([NSDate timeIntervalSinceReferenceDate] * 10000.0);
>
> NSLog(@"launched, seed = %ld", seed );
> srandom( seed );
Put a breakpoint on srandom() and setstate(). Maybe something else is re-seeding it (with a constant!) after you. If you want to be really paranoid, make sure that your calls to random() are really going to the libSystem routine, instead of being covered or hidden by a macro or a routine in another library.
Also, consider using srandomdev() instead of srandom(). It's more random, although slower, but probably not enough slower that you'd notice one call.
Regards,
Ken
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden