Main Page | Alphabetical List | Class List | File List | Class Members | File Members

rand.h File Reference

provides simple portable implementations of rand() and srand() More...

#include <nlibc.h>

Include dependency graph for rand.h:

Include dependency graph

This graph shows which files directly or indirectly include this file:

Included by dependency graph

Go to the source code of this file.

Functions

unsigned rand_r (unsigned *seed)
int rand (void)
vector int vrand (void)
void srand (unsigned int seed)
void vsrand (vector unsigned seed)


Detailed Description

provides simple portable implementations of rand() and srand()

Definition in file rand.h.


Function Documentation

int rand void   )  [inline]
 

NAME
rand, srand - random number generator.

SYNOPSIS
#include <stdlib.h>

int rand(void);
vector int vrand(void);

void srand(unsigned int seed);
void svrand(vector unsigned seed);

DESCRIPTION
The rand() function returns a pseudo-random integer
between 0 and RAND_MAX. The vrand() function returns a
vector integer.

The srand() function sets its argument as the seed for a
new sequence of pseudo-random integers to be returned by
rand(). These sequences are repeatable by calling srand()
with the same seed value. svrand() works in a similar way
but initialises high and low bank differently.

If no seed value is provided, the rand() function is auto­
matically seeded with a value of 1 (low bank) and 2 (high
bank.

RETURN VALUE
The rand() function returns a value between 0 and
RAND_MAX. The srand() returns no value.

NOTES
In Numerical Recipes in C: The Art of Scientific Computing
(William H. Press, Brian P. Flannery, Saul A. Teukolsky,
William T. Vetterling; New York: Cambridge University
Press, 1992 (2nd ed., p. 277)), the following comments are
made:
"If you want to generate a random integer between 1
and 10, you should always do it by using high-order
bits, as in

j=1+(int) (10.0*rand()/(RAND_MAX+1.0));

and never by anything resembling

j=1+(rand() % 10);

(which uses lower-order bits)."

Random-number generation is a complex topic. The Numeri­
cal Recipes in C book (see reference above) provides an
excellent discussion of practical random-number generation
issues in Chapter 7 (Random Numbers).

For a more theoretical discussion which also covers many
practical issues in depth, please see Chapter 3 (Random
Numbers) in Donald E. Knuth's The Art of Computer Program­
ming, volume 2 (Seminumerical Algorithms), 2nd ed.; Read­
ing, Massachusetts: Addison-Wesley Publishing Company,
1981.

IMPLEMENTATION for apeNEXT:
These function should NOT be considered to be a good RNG.

Definition at line 98 of file rand.h.

References rand_r().

unsigned rand_r unsigned *  seed  )  [inline]
 

Definition at line 19 of file rand.h.

Referenced by rand().

void srand unsigned int  seed  )  [inline]
 

Definition at line 122 of file rand.h.

vector int vrand void   )  [inline]
 

Definition at line 111 of file rand.h.

void vsrand vector unsigned  seed  )  [inline]
 

Definition at line 130 of file rand.h.


Generated on Mon Feb 20 15:54:35 2006 for nlibc by doxygen 1.3.5