home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / c / 12930 < prev    next >
Encoding:
Text File  |  1992-08-29  |  2.2 KB  |  71 lines

  1. Path: sparky!uunet!gatech!purdue!yuma!longs.LANCE.ColoState.EDU!tp718390
  2. From: tp718390@longs.LANCE.ColoState.EDU (Thomas Proell)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: random number generation
  5. Message-ID: <Aug28.205915.65889@yuma.ACNS.ColoState.EDU>
  6. Date: 28 Aug 92 20:59:15 GMT
  7. References: <1992Aug27.133805.21292@u.washington.edu>
  8. Sender: news@yuma.ACNS.ColoState.EDU (News Account)
  9. Reply-To: tp718390@longs.LANCE.ColoState.EDU (Thomas Proell)
  10. Organization: Engineering College, Colorado State University
  11. Lines: 58
  12.  
  13. In article <1992Aug27.133805.21292@u.washington.edu>,
  14. scipio@milton.u.washington.edu (jim lee) writes:
  15. > Can someone help me?  I need some coding that will generate random
  16. > numbers.
  17. > Thanks for any help.
  18. > Jim Lee (scipio@u.washington.edu)
  19.  
  20. A very elegant way to generate many different streams of uniformly
  21. distributed pseudo-random numbers is to use the ?rand48() library.
  22. The ? stands for either one of the following letters [d, e, l, n,
  23. m, j]. This assumes that you have access to a good C-compiler. 
  24.  
  25. drand48(), lrand48(), and mrand48() require to be initialized, i.e.
  26. srand48(time(NULL)); and are called without an argument. 
  27.  
  28. erand48(x sub i), nrand48(x sub i), and jrand48(x sub i) do not need
  29. to be initialized. x stands for an >unsigned short< integer array. Using
  30. different arrays as arguments for calling the subroutine gives you the 
  31. ability to generate different streams of i.e. floating point values; i.e: 
  32.  
  33. main(int argc, char **argv)
  34. {
  35.  
  36.   unsigned short *x, *y, ....
  37.  
  38.   float          *a, *b, ....
  39.  
  40.   x = calloc(MAX, sizeof(unsigned short)); 
  41.   y = .......
  42.   a = calloc(MAX, sizeof(float)); 
  43.   .....
  44.  
  45. /* Seed the first element. All subsequent values are generated 
  46.    by the function.                                               */
  47.  
  48.   x[0] = 0; 
  49.   y[0] = 10; 
  50.  
  51.   START LOOP
  52.       a[loop] = (float) nrand48(x); 
  53.       b[loop] = (float) nrand48(y); 
  54.   .
  55.   .
  56.  
  57. lrand48() and nrand48() gives a non-negative long integer in [0.0....2^31)
  58. mrand48() and jrand48() gives signed long integer in [-2^31....2^31]
  59. drand48() and erand48() gives a non-negative double precision in [0.0...1.0]
  60. {Had troubles with the last one!}
  61.  
  62.  
  63. Hope I could help, 
  64. Thomas Proell
  65. Department of Chemical Engineering
  66. Colorado State University
  67. Fort Collins, CO 80525  
  68.