Re: Seeding random() randomly
On Thu, May 26, 2011 at 9:15 PM, Dave Keck 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] * >> 1.0); >> >> NSLog(@"launched, seed = %ld", seed ); >> srandom( seed ); > > I'm not sure what your problem is, but I believe arc4random() has > superseded random() for a while now. Be careful with ARC4 since its output is biased. The bias makes it unsuitable for cryptographic/security related uses; and probably unsuitable for other purposes, such as simulations, where a unifirm distribution is usually needed. Jeff ___ 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
Re: Seeding random() randomly
On May 26, 2011, at 9:04 PM, Scott Ribe wrote: >> It’s never a good idea to seed a RNG with something guessable like this. > > Not all applications of random() have anything to do with security… Agreed. But I didn’t say it was always a bad idea, just never a good one. :) Seeding with something like a timestamp gives you results that are neither repeatable nor truly unguessable, and I can’t think of any situation where that would be a benefit. Especially when it’s more work than just calling srandomdev(). Besides, people tend to search for snippets of existing code and paste them into new code, and it would be sad if code like the above got moved from, say, a game to, say, a password generator. —Jens smime.p7s Description: S/MIME cryptographic 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
Re: Seeding random() randomly
Regarding format specifiers, there is the "String Format Specifiers" section of the "String Programming Guide" in Apple's Developer Documentation. Here is a link to the web version: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html%23//apple_ref/doc/uid/TP40004265-SW1 Todd ___ 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
Re: Seeding random() randomly
On May 26, 2011, at 10:32 PM, Jens Alfke wrote: > > On May 26, 2011, at 7:15 PM, Kevin Bracey wrote: > >> srandom(time(NULL)); > > It’s never a good idea to seed a RNG with something guessable like this. (An > old exploit against the Netscape browser’s SSL implementation was made > possible in part by doing exactly that.) > > All you have to do is call srandomdev() once; that will seed the generator > used by random() with some extremely random (“high-entropy”) data read from > /dev/random, which is generated by the kernel through all kinds of black > magic. It lacks 'good' confusion. http://en.wikipedia.org/wiki/Confusion_and_diffusion Sort of like the OP's seed multiply, which just led to 'bad' confusion. ___ 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
Re: Seeding random() randomly
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On 5/26/11 9:52 PM, Graham Cox wrote: > > On 27/05/2011, at 2:42 PM, Clark Cox wrote: > >> No. 'unsigned' is the same size on both 32- and 64-bit The only >> built-in types that are different between 32- and 64-bits are: >> >> signed long long (which is really the same as "signed long") >> unsigned long and any pointer type >> >> All other built-in types remain the same size between the two. > > > Thankyou - a definitive answer. I will print it out and frame it :) This is also why you should probably use NSInteger/NSUInteger and CGFloat, where appropriate (though perhaps not here). They are typedef'd (in NSObjCRuntime.h/CGBase.h) on a per-architecture basis. > This suggest that Conrad's analysis is not quite right then, since > even if I'm overflowing the 32-bit integer, there's still enough > variation in what remains to give a valid and usable seed: > > 2011-05-27 14:25:14.689 Squaresgame[83159:a0f] seed: 276132753 > 2011-05-27 14:25:14.691 Squaresgame[83159:a0f] random: 1826068185 > 2011-05-27 14:49:36.081 Squaresgame[83217:a0f] seed: 290746671 > 2011-05-27 14:49:36.083 Squaresgame[83217:a0f] random: 162579918 Them are fightin' words. (Just kidding.) When I ran my sample code on my machine, I got constant results, not what you observed above. I would presume that long int is converted to unsigned int by chopping off the excess bits on the left, as you indicated, but my C implementation knowledge is too shallow to be certain. Don't know why I got the results I did. There is also the issue that I mentioned in my previous message: at least the documentation indicate that srandom() expects an unsigned int (NOT a long, or an NSUInteger, or something else). I'm frankly a little confused by this (and this is the first time I noticed that), but if that's the case, then who knows that srandom() is going to do internally if handed something wacky? (I do realize that you are declaring unsigned, so this presumably is not the problem in this case, but is something to be aware of.) I guess what I conclude from all this from this is: the fact that we are having this conversation indicates that you probably shouldn't be going this route. Even if the behavior you are seeing is the correct result (say, defined in the C spec), relying on these idiosyncrasies leads to potentially confusing and hard to read code. And if the behavior is NOT consistent (there are certainly plenty of type conversions out there that give "undefined result"), then the situation is even worse. As far as you current problem goes, though, it seems like more than enough people have chimed in with good approaches. - -- Conrad Shultz Synthetiq Solutions www.synthetiqsolutions.com -BEGIN PGP SIGNATURE- Version: GnuPG/MacGPG2 v2.0.14 (Darwin) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iEYEARECAAYFAk3fTocACgkQaOlrz5+0JdVpsgCfU9gXPDY+wpU8i2dSFNUo0cSS NvoAoIVKvXYmn9luGqetCvNhQFrX5+2x =t3tC -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
Re: Seeding random() randomly
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] * > 1.0); > > NSLog(@"launched, seed = %ld", seed ); > srandom( seed ); unsigned seed = (unsigned)([NSDate timeIntervalSinceReferenceDate] * 1.0); sleep(2); unsigned seed2 = (unsigned)([NSDate timeIntervalSinceReferenceDate] * .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 (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
Re: Seeding random() randomly
On 27/05/2011, at 2:42 PM, Clark Cox wrote: > No. 'unsigned' is the same size on both 32- and 64-bit The only > built-in types that are different between 32- and 64-bits are: > > signed long > long (which is really the same as "signed long") > unsigned long > and any pointer type > > All other built-in types remain the same size between the two. Thankyou - a definitive answer. I will print it out and frame it :) This suggest that Conrad's analysis is not quite right then, since even if I'm overflowing the 32-bit integer, there's still enough variation in what remains to give a valid and usable seed: 2011-05-27 14:25:14.689 Squaresgame[83159:a0f] seed: 276132753 2011-05-27 14:25:14.691 Squaresgame[83159:a0f] random: 1826068185 2011-05-27 14:49:36.081 Squaresgame[83217:a0f] seed: 290746671 2011-05-27 14:49:36.083 Squaresgame[83217:a0f] random: 162579918 --GRaham ___ 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
Re: Seeding random() randomly
On Thu, May 26, 2011 at 9:30 PM, Graham Cox wrote: > > I should mention this is in a 64-bit build, so doesn't that mean that > 'unsigned' is 64 bits? No. 'unsigned' is the same size on both 32- and 64-bit The only built-in types that are different between 32- and 64-bits are: signed long long (which is really the same as "signed long") unsigned long and any pointer type All other built-in types remain the same size between the two. -- Clark S. Cox III clarkc...@gmail.com ___ 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
Re: Seeding random() randomly
On 27/05/2011, at 1:16 PM, Conrad Shultz wrote: > 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 1. > > 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 . This makes complete sense. However, it seems not to be the case. I found out why I'm getting the same random numbers each time - explain in a sec. But this code does *appear* to give random results (having fixed the format specifier issue): unsigned seed = (unsigned)([NSDate timeIntervalSinceReferenceDate] * 1.0); NSLog(@"seed: %u", seed ); srandom( seed ); long r = random(); NSLog(@"random: %ld", r ); I should mention this is in a 64-bit build, so doesn't that mean that 'unsigned' is 64 bits? However, the problem with a 32-bit build is definitely an issue, so I'll revise the code anyway. The x1 is an attempt (probably misguided) to ensure a truly varying seed since casting to an int would only give a value that changed every second because NSTimeInterval is a floating point value in seconds. That is in fact entirely sufficient - you can't launch the app more than once a second! The *real* reason I was getting the same sequence from random() is that this is a document-based app (actually a simple game, random is used to set up a variable initial game state) and the first document is created before -applicationDidFinishLaunching: is invoked. Creating the document loads the nib and initialises the data structures for the game, including the 'random' initial state. This order of initialisation during launch is something I've run into before, but had forgotten about - I was assuming that the app launched, then it opened the first document. Moving the seeding to -applicationWillFinishLaunching: instead solved the problem since this is definitely called prior to creating the first document. Thanks to everyone for helping straighten this out - 32/64 bit issues are still rather new to me. --Graham ___ 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
Re: Seeding random() randomly
On May 26, 2011, at 9:32 PM, Jens Alfke wrote: > It’s never a good idea to seed a RNG with something guessable like this. Not all applications of random() have anything to do with security... -- Scott Ribe scott_r...@elevated-dev.com http://www.elevated-dev.com/ (303) 722-0567 voice ___ 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
Re: Seeding random() randomly
On May 26, 2011, at 7:15 PM, Kevin Bracey wrote: > srandom(time(NULL)); It’s never a good idea to seed a RNG with something guessable like this. (An old exploit against the Netscape browser’s SSL implementation was made possible in part by doing exactly that.) All you have to do is call srandomdev() once; that will seed the generator used by random() with some extremely random (“high-entropy”) data read from /dev/random, which is generated by the kernel through all kinds of black magic. —Jens smime.p7s Description: S/MIME cryptographic 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
Re: Seeding random() randomly
I've seen gnu documentation for srandom that suggest the equivalent of Kevin's suggestion, namely "srandom(time(0))". Not sure if using the NSDate has any advantage over a call to time() and it would avoid this type of thing (gnu is likely to ensure time() and srandom() work correctly together). Aaron On May 26, 2011, at 8:10 PM, Chase Latta wrote: > unsigned seed = (unsigned)([NSDate timeIntervalSinceReferenceDate] * 1.0); > > You are trying to set seed to a value that is something like > 3,281,585,690,000; seed cannot handle this value so it will be set to > 4294967295, at least on my machine. > > You are using the same seed each time you run the program so you are > getting the same values. Try getting rid of the "* 1.0". > > Chase > > On Thu, May 26, 2011 at 7:15 PM, Kevin Bracey wrote: >> I think this was from some programming book I have, sorry I can site it: >> >> srandom(time(NULL)); >> >> cheers >> Kevin >> >> >> ___ >> >> 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/chaselatta%40gmail.com >> >> This email sent to chasela...@gmail.com >> > ___ > > 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/eeyore%40monsterworks.com > > This email sent to eey...@monsterworks.com > ___ 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
Re: Seeding random() randomly
-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] * > 1.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 1. 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
Re: Seeding random() randomly
unsigned seed = (unsigned)([NSDate timeIntervalSinceReferenceDate] * 1.0); You are trying to set seed to a value that is something like 3,281,585,690,000; seed cannot handle this value so it will be set to 4294967295, at least on my machine. You are using the same seed each time you run the program so you are getting the same values. Try getting rid of the "* 1.0". Chase On Thu, May 26, 2011 at 7:15 PM, Kevin Bracey wrote: > I think this was from some programming book I have, sorry I can site it: > > srandom(time(NULL)); > > cheers > Kevin > > > ___ > > 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/chaselatta%40gmail.com > > This email sent to chasela...@gmail.com > ___ 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
Re: Seeding random() randomly
I think this was from some programming book I have, sorry I can site it: srandom(time(NULL)); cheers Kevin ___ 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
Re: Seeding random() randomly
On 27/05/2011, at 11:53 AM, Ken Thomases wrote: > %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. Thanks Ken, very helpful. I just looked again at the 'string format specifiers' page in the docs. Seems it's been much fleshed out since I last looked, and sure enough it's now much clearer what to use when. --Graham ___ 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
Re: Seeding random() randomly
I don't think random/srandom are superseded. I use them myself for game creation. The game just has one number associated with it, I use that to seed random and I can completely recreate the whole game from that one number. I use the slightly more complicated initstate version. I see no reason why your method isn't working however. Two things to try. Print the first few randoms right after you seed to prove to yourself that you are seeding. Put a breakpoint on srandom, initstate and setstate to see if another piece of code is stomping yours. On May 27, 2011, at 9:32, 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] * >>> 1.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? 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. > > 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. > > --Graham___ > > 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/rols%40rols.org > > This email sent to r...@rols.org ___ 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
Re: Seeding random() randomly
Weird. Setting a breakpoint on srandom, setstate or srandomdev never triggers except for my own call of it. Using srandomdev doesn't change anything. Switching to arc4random() gives me a varying random sequence, so that's a usable solution to my problem. But the behaviour with random() I'm seeing is mysterious. --Graham On 27/05/2011, at 11:15 AM, Dave Keck 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] * >> 1.0); >> >>NSLog(@"launched, seed = %ld", seed ); >>srandom( seed ); > > I'm not sure what your problem is, but I believe arc4random() has > superseded random() for a while now. ___ 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
Re: Seeding random() randomly
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] * >>> 1.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 (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
Re: Seeding random() randomly
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] * >> 1.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? 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. 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. --Graham___ 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
Re: Seeding random() randomly
On May 26, 2011, at 6:15 PM, Dave Keck wrote: > I'm not sure what your problem is, but I believe arc4random() has superseded > random() for a while now. Incorrect. There are many reason to have a seedable PRNG, the least of which is the ability to reasonably debug the randomness. That an arc4random() is meant for cryptographic security, and as such using it suffers a performance tradeoff. As an example of "debugging the randomness" I recently created a graphical effect that relied heavily on random number generation. I needed to debug an issue that occurred 2 minutes into the sequence and relatively rare. The only reason I could reasonably debug the issue like this was because I had control over the sequence of numbers that I was seeing. -- David Duncan ___ 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
Re: Seeding random() randomly
On May 26, 2011, at 18:00, Graham Cox wrote: > unsigned seed = (unsigned)([NSDate timeIntervalSinceReferenceDate] * > 1.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. ___ 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
Re: Seeding random() randomly
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] * > 1.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 (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
Re: Seeding random() randomly
> 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] * > 1.0); > > NSLog(@"launched, seed = %ld", seed ); > srandom( seed ); I'm not sure what your problem is, but I believe arc4random() has superseded random() for a while now. ___ 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
Seeding random() randomly
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] * 1.0); NSLog(@"launched, seed = %ld", seed ); srandom( seed ); --Graham ___ 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