home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!gatech!purdue!yuma!longs.LANCE.ColoState.EDU!tp718390
- From: tp718390@longs.LANCE.ColoState.EDU (Thomas Proell)
- Newsgroups: comp.lang.c
- Subject: Re: random number generation
- Message-ID: <Aug28.205915.65889@yuma.ACNS.ColoState.EDU>
- Date: 28 Aug 92 20:59:15 GMT
- References: <1992Aug27.133805.21292@u.washington.edu>
- Sender: news@yuma.ACNS.ColoState.EDU (News Account)
- Reply-To: tp718390@longs.LANCE.ColoState.EDU (Thomas Proell)
- Organization: Engineering College, Colorado State University
- Lines: 58
-
- 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.
- >
- > Thanks for any help.
- >
- >
- > Jim Lee (scipio@u.washington.edu)
-
- A very elegant way to generate many different streams of uniformly
- distributed pseudo-random numbers is to use the ?rand48() library.
- The ? stands for either one of the following letters [d, e, l, n,
- m, j]. This assumes that you have access to a good C-compiler.
-
- drand48(), lrand48(), and mrand48() require to be initialized, i.e.
- srand48(time(NULL)); and are called without an argument.
-
- erand48(x sub i), nrand48(x sub i), and jrand48(x sub i) do not need
- to be initialized. x stands for an >unsigned short< integer array. Using
- different arrays as arguments for calling the subroutine gives you the
- ability to generate different streams of i.e. floating point values; i.e:
-
- main(int argc, char **argv)
- {
-
- unsigned short *x, *y, ....
-
- float *a, *b, ....
-
- x = calloc(MAX, sizeof(unsigned short));
- y = .......
- a = calloc(MAX, sizeof(float));
- .....
-
- /* Seed the first element. All subsequent values are generated
- by the function. */
-
- x[0] = 0;
- y[0] = 10;
-
- START LOOP
- a[loop] = (float) nrand48(x);
- b[loop] = (float) nrand48(y);
- .
- .
-
- lrand48() and nrand48() gives a non-negative long integer in [0.0....2^31)
- mrand48() and jrand48() gives signed long integer in [-2^31....2^31]
- drand48() and erand48() gives a non-negative double precision in [0.0...1.0]
- {Had troubles with the last one!}
-
-
- Hope I could help,
- Thomas Proell
- Department of Chemical Engineering
- Colorado State University
- Fort Collins, CO 80525
-