• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: rand() and srand() broken?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: rand() and srand() broken?


  • Subject: Re: rand() and srand() broken?
  • From: Derek Gaston <email@hidden>
  • Date: Wed, 25 Nov 2009 11:40:10 -0700

On Nov 25, 2009, at 10:47 AM, Jason Foreman wrote:


On Nov 25, 2009, at 11:26 AM, Derek Gaston wrote:

This piece of code should generate 100 random numbers between 0 and 1... I am expecting that they be fairly spaced out as well...

What basis do you have for this expectation? Is it spelled out in any specification or standard somewhere?


You're seeding the RNG with a monotonically increasing seed, and getting what looks like monotonically increasing starting values for your random sequences. The fact that you get what you expect on Linux seems to be purely coincidental.

Are you (and everyone else that replied) trying to tell me that the first number out of rand() (after reseeding) should always be the predictable? Because that's essentially what I'm seeing. This, to me, feels very wrong. If I change the seed.... I should see a different first number. Yes... I agree that later numbers will possibly look more random (depending on the algorithm used). But again, if the seed has changed... the first number out of rand() should be _different_. OSX is the only OS acting this way.


It's really hard for me to believe that the first number out of rand after calling srand is so predictable. It seems like a poor choice of PRNG algorithm...

This is actually indicative of what we actually do in one of our applications (I know it doesn't usually make much sense to reseed all the time... but you're just going to have to trust me that it's necessary in our case ;-)


I think you're going to have to explain why you feel reseeding is necessary. You probably shouldn't be doing it.

Heh - I knew this would come up. The issue is that we need to be able to reliably _regenerate_ different random numbers over and over.... but on a grand scale. Think of a matrix... you need to be able to get a random number for each entry in the matrix (let's say it's 10000000x10000000... so a LOT of numbers). BUT... you don't want to _store_ all of those numbers.... you just want to be able to query what the random number for any given entry is. So we come up with a way of guaranteeing unique seed numbers (for instance, using combinations of row and column numbers for this matrix example). Then we can always query a place in the matrix just by reseeding and calling rand(). It will reliably always give us back the same number over and over for a given seed.


Our actually application is much more complicated than this... but I think this demonstrates our general need.

Also... as someone else mentioned I did try using random() instead... and it works fine... but is CRAZY slow. It easily takes over 80% of the runtime of our application! (where rand() is in the noise). Yes... this means it's using a better algorithm... but there has to be a balance. On Linux rand() works correctly _and_ is fast.


_______________________________________________ Do not post admin requests to the list. They will be ignored. Darwin-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
  • Follow-Ups:
    • Re: rand() and srand() broken?
      • From: Amanda Walker <email@hidden>
    • Re: rand() and srand() broken?
      • From: Clark Cox <email@hidden>
    • Re: rand() and srand() broken?
      • From: "Daniel M. Zimmerman" <email@hidden>
References: 
 >rand() and srand() broken? (From: Derek Gaston <email@hidden>)
 >Re: rand() and srand() broken? (From: Jason Foreman <email@hidden>)

  • Prev by Date: Re: rand() and srand() broken?
  • Next by Date: Re: rand() and srand() broken?
  • Previous by thread: Re: rand() and srand() broken?
  • Next by thread: Re: rand() and srand() broken?
  • Index(es):
    • Date
    • Thread