Re: Using C's rand() to generate a random number
- From: Brian S <brianscsmith@xxxxxxxxx>
- Date: Fri, 13 Jul 2007 08:25:57 -0700
On Jul 11, 4:36 pm, Jason Pawloski <jpawlo...@xxxxxxxxx> wrote:
Hello, I need to generate 5 random numbers, some between 0 to 23,
another between 0 and 24 and yet another between 0 and 25.
I am using this:
int getRandomNumber(int maxn)
{
return (unsigned int)(rand() % maxn);
}
The computations I am doing are extremely sensitive and I believe this
random number generate to be biased. Since rand() returns a number
between 0 and 32767, and not all 23, 24 and 25 can possibly divide
32767, some are more likely to be chosen than others.
In this program, I am eventually generating 500,000,000 random
numbers, so this bias will likely to be seen.
I am not a programmer by trade so I don't really know a lot, but is
there an easy way to get a unbiased random number?
Thanks! Jason
I would recommend Mersenne Twister, which is good for trillions of
random 32 bit numbers. I use it for any application where I need more
than a few random numbers.
.
- References:
- Using C's rand() to generate a random number
- From: Jason Pawloski
- Using C's rand() to generate a random number
- Prev by Date: Re: Ultimate debunking of Cantor's Theory
- Next by Date: Re: Ultimate debunking of Cantor's Theory
- Previous by thread: Re: Using C's rand() to generate a random number
- Next by thread: Complete Solution Manual in .pdf to textbooks. Get it in Hours!
- Index(es):
Relevant Pages
|