Re: Is rand() not that random??
Re: Is rand() not that random??
- Subject: Re: Is rand() not that random??
- From: David Remahl <email@hidden>
- Date: Mon, 10 Dec 2001 16:52:53 +0100
>
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...
>
When I take the whole number returned by rand I don't seem to be
>
able to make up a pattern but if I take its modulus 4, it just
>
returns 2 3 0 1 2 3 0 1... (only starting with a random number).
>
>
>
Below the code and the output in this example X is 4 but it
>
works with other numbers as well... :)
>
Code:
>
I'm aware of the implementation problems that a pseudo random
>
generator brings, but still this doesn't seem to me as a
>
particularly clever way of doing it... :/ Or maybe I am totally
>
wrong...
>
If one of you has a better way of getting a random number
>
between 0 and X... let me know.
>
>
Thanks,
>
Alex.
I suggest you take a look at macosx-dev at OmniGroup, where this issue was
thoroughly dissected a few weeks ago (starting Nov 5th)
/ david