Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!cs.utexas.edu!tut.cis.ohio-state.edu!uc!shamash!paul From: paul@u02.svl.cdc.com (paul kohlmiller) Newsgroups: comp.lang.c Subject: Re: random number generator Keywords: random number Message-ID: <23758@shamash.cdc.com> Date: 24 Jul 90 19:04:35 GMT References: <941@dgis.dtic.dla.mil> <1990Jul23.192539.5587@cs.utk.edu> Sender: news@shamash.cdc.com Distribution: usa Lines: 26 wozniak@utkux1.utk.edu (Bryon Lape) writes: >In article <941@dgis.dtic.dla.mil> tswenson@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