Re: Seeding random() randomly
Re: Seeding random() randomly
- Subject: Re: Seeding random() randomly
- From: Ken Thomases <email@hidden>
- Date: Thu, 26 May 2011 20:53:37 -0500
On May 26, 2011, at 8:32 PM, Graham Cox wrote:
>
> On 27/05/2011, at 11:23 AM, Quincey Morris wrote:
>
>> On May 26, 2011, at 18:00, Graham Cox wrote:
>>
>>> unsigned seed = (unsigned)([NSDate timeIntervalSinceReferenceDate] * 10000.0);
>>>
>>> NSLog(@"launched, seed = %ld", seed );
>>
>> Also, be careful here, because %ld is the wrong format specifier for type 'unsigned'. Whether it logs the right value is going to be architecture dependent.
>
> Ok, then that raises the question what should I use?
%u
> I'm confused about how to correctly write format specifiers for both 32 and 64-bit runtimes. The 64-bit porting guide doesn't spell it out (yet you end up with code peppered with warnings that you should examine the use of the format specifier without docs properly explaining their correct use). It's also not clear to me whether just 'unsigned' is a fixed-size quantity or not, depending on architecture.
Just "unsigned" is shorthand for "unsigned int". Between the Mac's 32-bit and 64-bit architectures, int doesn't change size. That can't be generalized, although it's darn-near universal.
However, that's irrelevant. The format specifiers don't indicate a size. They indicate a type. For "unsigned", you use "%u". That's the end of the story.
> Someone mentioned that random() has been superseded. Again??! It seems to me that random number generators get superseded every other week. How is anyone supposed to know what is considered current best practice? Especially as for such functions there doesn't seem to be a simple way to see in man pages or other documentation what's deprecated.
random() has not been deprecated or even superseded. arc4random() has higher quality pseudo-randomness, which is probably what prompted the claim, but as David Duncan said that doesn't justify the claim. It's a question of tradeoffs. arc4random() is slower and non-reproducible. (The slowness may matter in this case, where I asserted it doesn't matter when seeding, because you usually seed once and generate many times.)
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