home *** CD-ROM | disk | FTP | other *** search
- Path: cs.ruu.nl!usenet
- From: wsldanke@cs.ruu.nl (Wessel Dankers)
- Newsgroups: comp.sys.amiga.programmer
- Subject: Re: Random #s
- Date: 23 Mar 96 02:20:12 +0100
- Organization: Dept of Computer Science, Utrecht University, The Netherlands
- Message-ID: <1496.6656T140T637@cs.ruu.nl>
- References: <4ihkif$ed0@apollo.isisnet.com> <4irnj8$a2t@dunlop.cs.strath.ac.uk>
- NNTP-Posting-Host: anx1p16.cc.ruu.nl
- X-Newsreader: THOR 2.22 (Amiga TCP/IP)
-
- Paul Callaghan <pcallagh@cs.strath.ac.uk> wrote:
- > Here is a little program which will generate random numbers
- > (Off the top of my head so it `should` work.)
- > (rand() % MUTATE)+
- > #include <time.h>
- > #define HI 6
- > #define LO 1
-
-
- > void main()
- > {
- > int rand_no;
-
- > srand(time(NULL));
-
- > rand_no=(rand() % HI)+LO;
- > }
-
- One of the most commonly used (but not one of the best) methods of generating
- pseudorandom numbers is the linear congruential method, which works as
- follows. Let x_0 be an initial number (the "seed")> The sequence is generated
- recursively as
-
- x_n = (ax_n-1 + c) mod m
-
- Making good choises of a, c and m involves both art and theory. The following
- are some values that have been proposed:
- (1) a = 69069, c = 0, m = 2^31
- (2) a = 65539, c = 0, m = 2^31
- The latter is an infamous generator called RANDU.
-
- AFAIK a better one is:
-
- /* returns a random number x | a <= x < b */
- unsigned int my_rand(void)
- { static unsigned int seed = 0x47E25FC4;
- return seed = (seed >> 1) | ((((seed&1)^((seed & 2)>>1)))<<31);
- }
-
- I hope there are no pesky sequence points involved this time :-)
-
- --
- Wessel Dankers _\\|//_ <wsldanke@cs.ruu.nl>
- ///|\\\
- ----------------------------oOO--(_)---OOo----------------------------
- `Never imagine yourself not to be otherwise than what it might appear
- to others that what you were or might have been was not otherwise than
- what you had been would have appeared to them to be otherwise.'
-
-