Re: Seeding random() randomly
Re: Seeding random() randomly
- Subject: Re: Seeding random() randomly
- From: Michael Hall <email@hidden>
- Date: Thu, 26 May 2011 21:26:19 -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 );
unsigned seed = (unsigned)([NSDate timeIntervalSinceReferenceDate] * 10000.0);
sleep(2);
unsigned seed2 = (unsigned)([NSDate timeIntervalSinceReferenceDate] * 9999.0);
if (seed == seed2)
NSLog(@"match %u %u",seed,seed2);
else
NSLog(@"mismatch %u %u",seed,seed2);
2011-05-26 21:24:23.339 SRandom[885:80f] match 4294967295 4294967295
_______________________________________________
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