• 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: [APPL:DarwinDev] rand() and srand() broken?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [APPL:DarwinDev] rand() and srand() broken?


  • Subject: Re: [APPL:DarwinDev] rand() and srand() broken?
  • From: "Jay A. Kreibich" <email@hidden>
  • Date: Wed, 25 Nov 2009 11:42:36 -0600

On Wed, Nov 25, 2009 at 10:26:51AM -0700, Derek Gaston scratched on the wall:
> Hello everyone... I've been struggling with rand and srand on OSX 10.6 and 10.5.  It seems to me that something is terribly broken.  Here is a short program I wrote to convince myself of that:
>
> #include <stdlib.h>
> #include <iostream>
>
> int main()
> {
>  for(unsigned int i=0; i<100; i++)
>  {
>    srand(i+1);
>    double value = rand()/(RAND_MAX + 1.0);
>    std::cout<<value<<std::endl;
>  }
> }
>
> This piece of code should generate 100 random numbers between
> 0 and 1... I am expecting that they be fairly spaced out as well...

  I wouldn't trust it to do that.  A good PRNG generates *sequences*
  that are well spaced and seemingly random.  You don't have sequences.

> 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 ;-)

  Not only does it not make sense, as this code shows, it is a very
  bad idea.  You're setting the seed over and over with very similar
  numbers (in the scope of a full unsigned integer) and getting similar
  first values.  Not exactly a surprise.  Since this isn't a sequence,
  there is little reason to assume they'd be fairly spaced out.  You
  need a sequence off a single seed for that.

> Am I doing something wrong??

  Yeah, stop re-seeding the PRNG.  Do it once on startup and never
  again.  Or, find a much better source of semi-random seeds.

  Clearly the Linux implementation does a better job at producing
  first-value numbers that cover the number domain a bit better.  But
  that doesn't discount the fact you're using the PRNG incorrectly.

    -j

--
Jay A. Kreibich < J A Y  @  K R E I B I.C H >

"Our opponent is an alien starship packed with atomic bombs.  We have
 a protractor."   "I'll go home and see if I can scrounge up a ruler
 and a piece of string."  --from Anathem by Neal Stephenson
 _______________________________________________
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

References: 
 >rand() and srand() broken? (From: Derek Gaston <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