-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On 5/26/11 6: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 );
(See end of message for a download URL for a sample project.) I'm pretty sure your problem has to do with overflowing your data types. "unsigned" is getting treated as "unsigned int", which will be too small to hold the value after you, for some reason I'm not clear on, multiply by 10000. When I wrote some sample code that used NSUInteger instead, everything worked properly. When I compiled using 32-bit settings (instead of native), the time interval produced using your code was wrong (and constant, explaining your problem). This makes sense: in 32 bit, NSUInteger is typedef'd to unsigned int, in 64 it is unsigned long, which _can_ accommodate . The srandom() manpage indicate it expects an unsigned (int) but that the legacy (BSD) version expects an unsigned long. This might be worth paying attention to as well. I have posted a working sample project (using NSUInteger, and with a GUI and all!) to http://dl.dropbox.com/u/5847625/RandomTest.zip - -- Conrad Shultz Synthetiq Solutions www.synthetiqsolutions.com -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (Darwin) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iD8DBQFN3xeCaOlrz5+0JdURAt1rAJ9m+JNb/Fd5fKOB8Icrg4yvSON3/QCdFp8F zV9DgdPunL5cUujQm/SKX4s= =+Yjz -----END PGP SIGNATURE----- _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com