random number generator

paul kohlmiller paul at u02.svl.cdc.com
Wed Jul 25 05:04:35 AEST 1990


wozniak at utkux1.utk.edu (Bryon Lape) writes:

>In article <941 at dgis.dtic.dla.mil> tswenson at dgis.dtic.dla.mil (Timothy Swenson) writes:
>>
>>	Does anyone have a random number generator
>>written in C?  Or better yet, Small-C.  I really need
>>one for a project I'm working on, and my compiler does
>>not have one in it's library.  I'll take anything that 

>	I thought that rand() and srand() are standard library
>functions!  These will generate random numbers.  srand() is the seed
>function while rand() returns a pseudo-random number.

Indeed the standard does define rand and srand and even puts the code in the
standard itself.
static unsigned long int next = 1;
int rand(void) /* RAND-MAX assumed to be 32767 */
{
next=next*1103515245+12345;
return (unsigned int) (next/65536) % 32768;
}
The srand function simply resets the value of next.
This code is an example only and it is not a requirement that an ANSI 
conforming implementation use this version of rand.
Paul Kohlmiller
standard disclaimers



More information about the Comp.lang.c mailing list