Re: Is rand() not that random??
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