Random number generator in Cpp

by PRanesh 2012-06-06 11:16:17


C provides random number generation function rand() that is found in <stdlib.h> header.

consider the following C statement:


i = rand();


The rand function generates an integer between 0 and RAND_MAX
(a constant defined in the <stdlib.h> header).
Standard C states that the value of RAND_MAX must be at least 32767, which is the maximum value for a two-byte (i.e., 16-bit) integer.

The value of the RAND_MAX differs from one compiler to another you can check the exact value of the RAND_MAX for your compiler simply by the following code.

#include <stdlib.h>
#include <stdio.h>
/* function main begins program execution */
int main() {

printf("%d", RAND_MAX);
return 0; /* indicates successful termination */
} /* end main */
816
like
0
dislike
0
mail
flag

You must LOGIN to add comments