home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / c / 12909 < prev    next >
Encoding:
Internet Message Format  |  1992-08-29  |  1.7 KB

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