• 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: Is rand() not that random??
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Is rand() not that random??


  • Subject: Re: Is rand() not that random??
  • From: Marco Scheurer <email@hidden>
  • Date: Mon, 10 Dec 2001 17:03:31 +0100

On Monday, December 10, 2001, at 04:20 am, Alexandre Aybes wrote:

Hi there I am having some difficulties getting random numbers...
I know this question is not really a cocoa-dev question, but I thought someone here might have a good answer to my problem :)) And I am using cocoa for my app... uhhh... anyay...
I want to compute random numbers from 1 to X (X being an arbitrary value computed at runtime). This sounds like a very simple problem and I thought it would take 2 seconds to implement, and I took the quick and durty path by just computing it so: 'rand() % X' I thought that it would work... but it doesn't seem to...[...]

No, rand() is not very random.

random() is a better choice, or you should take rand() higher bits.

And there was a discussion about this a while ago on macosx-
email@hidden. I mentionned another pseudo random number generator called the Mersenne Twister: http://www.math.keio.ac.jp/~matumoto/emt.html .

The SenFoundation framework, available in the distribution of OCUnit (http://www.sente.ch/software/ocunit), includes an implementation of the Mersenne Twister in the SenRandom class (loosely modeled after Java's Random).

Marco Scheurer
Sen:te, Lausanne, Switzerland http://www.sente.ch


References: 
 >Is rand() not that random?? (From: Alexandre Aybes <email@hidden>)

  • Prev by Date: Re: How to get scripting to work without NSApplication
  • Next by Date: Re: Is rand() not that random??
  • Previous by thread: Is rand() not that random??
  • Next by thread: Re: Is rand() not that random??
  • Index(es):
    • Date
    • Thread