rand

Description

Generates a pseudorandom number.

#include <stdlib.h> Required only for function declarations  

int rand( void );

Remarks

The rand function returns a pseudorandom integer in the range 0 to RAND_MAX. The srand routine can be used to seed the pseudorandom-number generator before calling rand.

Return Value

The rand function returns a pseudorandom number, as described above. There is no error return.

Compatibility

Standards:ANSI, UNIX

16-Bit:DOS, QWIN, WIN, WIN DLL

32-Bit:DOS32X

See Also

srand

Example

/* RAND.C: This program seeds the random-number generator with the

* time, then displays 20 random integers.

*/

#include <stdlib.h>

#include <stdio.h>

#include <time.h>

void main( void )

{

int i;

/* Seed the random-number generator with current time so that

* the numbers will be different every time we run.

*/

srand( (unsigned)time( NULL ) );

/* Display 10 numbers. */

for( i = 0; i < 10; i++ )

printf( “ %6d\n”, rand() );

}

Output

19471

16395

8268

15582

6489

28356

27042

5276

23070

10930