[ First ]  [ Previous ]  [ Next ]  [ Last ]  [ Manuals ]


rand

Generate a pseudo-random integer value.

Compatibility:

This function is compatible with the following targets:

ANSI

BeOS

EMB/RTOS

Mac OS

Palm OS

Win32


Prototype:
#include <stdlib.h>
int rand(void);
Parameters:

None

Remarks:

A sequence of calls to the rand() function generates and returns a sequence of pseudo-random integer values from 0 to RAND_MAX. The RAND_MAX macro is defined in stdlib.h.

By seeding the random number generator using srand(), different random number sequences can be generated with rand().

Return:

rand() returns a pseudo-random integer value between 0 and RAND_MAX.

See Also:

"srand"

Example of rand() usage.:
#include <stdlib.h>
#include <stdio.h>

int main(void)
{
	int i;
	unsigned int seed;
	
	for (seed = 1; seed <= 5; seed++) {
		srand(seed);
		printf("First five random number for seed %d:\n",
				seed);
		for (i = 0; i < 5; i++)
			printf("%10d", rand());
		printf("\n\n");		// terminate the line
	}

	return 0;
}

Output:
First five random number for seed 1:
     16838      5758     10113     17515     31051

First five random number for seed 2:
       908     22817     10239     12914     25837

First five random number for seed 3:
     17747      7107     10365      8312     20622

First five random number for seed 4:
      1817     24166     10491      3711     15407

First five random number for seed 5:
     18655      8457     10616     31877     10193


[ First ]  [ Previous ]  [ Next ]  [ Last ]  [ Manuals ]

Visit the Metrowerks website at: http://www.metrowerks.com
For assistance contact Metrowerks Technical Support at: cw_support@metrowerks.com
Copyright © 2000, Metrowerks Corp. All rights reserved.

Last updated: August 16, 2000