Re: How to generate random numbers in C
- From: user923005 <dcorbit@xxxxxxxxx>
- Date: Fri, 11 Apr 2008 12:48:16 -0700 (PDT)
On Apr 11, 12:20 pm, gordonb.k5...@xxxxxxxxxxx (Gordon Burditt) wrote:
I need to generate two uniform random numbers between 0 and 1 in C ?
Computers do not generate truly random numbers without hardware support.
Some techniques include the detection of radioactive decay and thermal
noise from a reverse-biased diode. There is some belief that there
is randomness in the timing (say, down to picoseconds) of keystrokes,
although I don't think anyone has managed to tie human typing to quantum
effects yet. Some CPU chips have hardware for random number generation
on them.
You may want pseudo-random numbers. In cryptography, random numbers
are very important and the difference between pseudo-random numbers
and real random numbers used in encryption may get you killed as a
spy.
If you try to offer casino gambling games (e.g. craps, blackjack,
roulette, etc.) for real money using pseudo-random numbers, you're
going to lose.
The most important factor in gambling is the size of the house.
In a fair game, the player with the biggest house money volume wins.
Assuming a Markov process (random walk) the players will get ahead and
behind in a random, wobbling fashion. But as soon as the cash for one
player is gone, the game is over. If you have one hundred dollars and
the opponent has one trillion dollars, you are in a lot of trouble.
That's why I call gambling "A tax on stupidity." Of course if you
have a trillion dollars it's a good idea, but it isn't very nice in
that case.
There are PRNGs with a period so large it would not repeat if every
computer on earth ran it at full speed until the silicon wore out.
E.g. check out the period of the Mersenne Twister or WELL PRNGs.
rand() returns the same sequence of numbers each time the program
starts up unless you call srand() with a different seed value from
the last time. Seed values are commonly derived from the time and/or
process ID (but NOT in applications where real random numbers are needed,
like gambling or cryptography: 32 bits of randomness isn't enough, and
a poor seed can cripple a good random number generator).
Another problem is that it is not always possible to know if a seed is
poor or not. Zero is a typical PRNG buster.
How to do it ?
There are better pseudo-random number generators than rand().
These have the disadvantage that they are not included in all C
libraries.
I looked into rand function where you need to #define RAND_MAX as 1
You do not get to redefine RAND_MAX. Also, the return type of rand()
is int, so don't expect any values where 0 < rand() < 1 . If RAND_MAX
were 1 (not allowed by the standard) all you would get is 0 and 1.
but will this rand function give me uniformly distributed and unique
numbers ?
Consider using algebra. rand() returns [0, RAND_MAX]. You want
numbers between 0.0 and 1.0 including both endpoints. Or is that just
including 0.0 but not 1.0? Consider what these might give you:
((double)rand())/RAND_MAX
or
((double)rand())/(RAND_MAX+1)
The problem with these [0,1] and [0,1) PRNGs is that they are *very*
grainy. They only give approximately RAND_MAX distinct values. If
you want something that is uniform between 0 and 1, it is likely to be
problematic unless only a very low quality PRNG is needed.
.
- Follow-Ups:
- Re: How to generate random numbers in C
- From: Bill Reid
- Re: How to generate random numbers in C
- From: Gordon Burditt
- Re: How to generate random numbers in C
- References:
- How to generate random numbers in C
- From: pereges
- Re: How to generate random numbers in C
- From: Gordon Burditt
- How to generate random numbers in C
- Prev by Date: Re: Why do people say 'extern' is error prone ?
- Next by Date: Re: How to generate random numbers in C
- Previous by thread: Re: How to generate random numbers in C
- Next by thread: Re: How to generate random numbers in C
- Index(es):