delorie.com/djgpp/doc/libc/libc_637.html | search |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
#include <stdlib.h> int rand(void); |
Returns a pseudo-random number between zero and RAND_MAX
(defined
on `stdlib.h').
By default, this function always generates the same sequence of numbers
each time you run the program. (This is usually desirable when
debugging, or when comparing two different runs.) If you need to
produce a different sequence on every run, you must seed rand
by
calling srand
(see section srand) before the first call to
rand
, and make sure to use a different argument to srand
each time. The usual technique is to get the argument to srand
from a call to the time
library function (see section time), whose
return value changes every second.
To get a random number in the range 0..N, use rand()%(N+1)
. Note
that the low bits of the rand
's return value are not very random,
so rand()%N
for small values of N could be not enough
random. The alternative, but non-ANSI, function random
is
better if N is small. See section random.
The number.
ANSI/ISO C | C89; C99 |
POSIX | 1003.2-1992; 1003.1-2001 |
/* random pause */ srand(time(0)); for (i=rand(); i; i--); |
webmaster | delorie software privacy |
Copyright © 2004 | Updated Apr 2004 |