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

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


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

On Wed, Nov 25, 2009 at 10:45:42AM -0800, Jens Alfke scratched on the wall:
>
> On Nov 25, 2009, at 9:26 AM, Derek Gaston wrote:
>
>> 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.
>
> You shouldn't be using rand; it's not a good PRNG and it was obsoleted
> ages ago by random().
> Even the man page tells you not to use it:
>
> RAND(3)                  BSD Library Functions Manual
> RAND(3)
>
> NAME
>      rand, rand_r, srand, sranddev -- bad random number generator
> ...
> DESCRIPTION
>      These interfaces are obsoleted by random(3).

  This is, I suspect, part of the problem.  When random() replaced
  rand() in the BSD sources ages ago, I'm guessing nobody ever went
  back and made rand() better.  It was understood that you just didn't
  use rand() and "fixing" it by changing the algorithm would break
  applications like yours.

  This is less true in Linux.  For one thing, the source code has a
  much younger lineage, and I'm guessing someone decided it was a good
  idea to "improve" rand() at some point, even if that meant breaking
  backwards compatibility.

  Have you tried you test on a BSD-based system?


  Regardless, in that situation I'd write my own PRNG.  That way you
  can exactly control the balance between performance and quality and,
  more importantly, you'll have cross-platform compatibility.  From what
  you're doing, you don't really need a "random" number, you just need
  a good hash that will go from regular numbers (row/column) to a good
  coverage of the number domain.  Even something like a CRC might be a
  good choice... unlike many crypto hashes, it does well with just a
  few bytes of data to chew on.

   -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

  • Follow-Ups:
    • Re: [APPL:DarwinDev] Re: rand() and srand() broken?
      • From: Derek Gaston <email@hidden>
References: 
 >rand() and srand() broken? (From: Derek Gaston <email@hidden>)
 >Re: rand() and srand() broken? (From: Jens Alfke <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: [APPL:DarwinDev] Re: rand() and srand() broken?
  • Index(es):
    • Date
    • Thread