home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!uwm.edu!ux1.cso.uiuc.edu!roundup.crhc.uiuc.edu!jobim.crhc.uiuc.edu!svleest
- From: svleest@jobim.crhc.uiuc.edu (Steve VanderLeest)
- Newsgroups: comp.lang.c
- Subject: Re: random number generation
- Date: 28 Aug 1992 13:43:24 GMT
- Organization: Center for Reliable and High-Performance Computing, University of Illinois at Urbana-Champaign
- Lines: 26
- Distribution: world
- Message-ID: <17lahsINNr38@roundup.crhc.uiuc.edu>
- References: <1992Aug27.133805.21292@u.washington.edu>
- NNTP-Posting-Host: jobim.crhc.uiuc.edu
-
- In article <1992Aug27.133805.21292@u.washington.edu>, scipio@milton.u.washington.edu (jim lee) writes:
- |> Can someone help me? I need some coding that will generate random
- |> numbers.
-
- Most decent C libraries have two routines called rand() and srand() which will
- do this for you.
-
- srand(seed) takes a seed number to start up the pseudo-random sequence
- rand() returns a random number once srand() has been used to seed
- the sequence
-
- If you do not have access to a decent library, the algorithm is rather simple.
-
- r[0] = MULTIPLIER*seed + ADDER
- r[n+1] = MULTIPLIER*r[n] + ADDER
-
- where MULTIPLIER is of the form 1+4k, k being an integer
- and ADDER is an odd integer. Make MULTIPLIER and ADDER large numbers
- or the sequence increases at first. Also, all operations are done
- mod the size of r[0]. That is, you must prevent the adds and multiplies
- from holding on to any carry or overflow.
-
- If the same seed is chosen, the same sequence will be repeated. The trick
- is to use a different seed each time. Best way is to derive seed from the
- system clock. If no clock is available, then you must input a different
- seed each time from another source.
-