Re: How to generate random numbers in C



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.

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).

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)

.



Relevant Pages

  • Re: How to generate random numbers in C
    ... Computers do not generate truly random numbers without hardware support. ... You may want pseudo-random numbers. ... rand() returns the same sequence of numbers each time the program ... process ID (but NOT in applications where real random numbers are needed, ...
    (comp.lang.c)
  • Re: random lottery draw funciton
    ... from approximately 18,000 shares. ... I believe rand() uses your operating system's default randsystem call, ... the quality of the pseudo-random numbers depends on your system. ... http://www.andyhsoftware.co.uk/space:: disk and FTP usage analysis tool ...
    (comp.lang.php)
  • Re: Truly random?
    ... Computers aren't very good at generating truly ... If you need truly random numbers for cryptography, a PRNG won't ... If calling rand() or randomalone slows down ... a pseudo-random number. ...
    (comp.lang.c)
  • Re: Way for computing random primes in standard C.
    ... number which is also pseudo-random? ... probabilities for all the primes that are at most 32 bits. ... Does it mean that further calls to rand() will return numbers with a new ... how is it different to calling srand() with a ...
    (comp.lang.c)
  • Re: Why will this not generate a random number!?
    ... >education is mainly based on object oriented design theory where Java ... I cannot for the life of me generate a random number. ... rand() does not generate random numbers. ... How do you know the result you got is NOT pseudo-random? ...
    (comp.lang.c)