home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / sys / amiga / programmer / 3490 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.7 KB

  1. Path: cs.ruu.nl!usenet
  2. From: wsldanke@cs.ruu.nl (Wessel Dankers)
  3. Newsgroups: comp.sys.amiga.programmer
  4. Subject: Re: Random Number Generation
  5. Date: 20 Feb 96 23:52:58 +0100
  6. Organization: Dept of Computer Science, Utrecht University, The Netherlands
  7. Message-ID: <2108.6624T1432T2901@cs.ruu.nl>
  8. References: <199602071623.QAA00075@sable.ox.ac.uk> <Pine.OSF.3.91.960207175121.20357B-100000@hai.hiMolde.no>
  9.      <19960207.41F660.11A72@aj050.du.pipex.com> <1100.6613T1273T666@himolde.no>
  10.     <692.6619T1254T1752@in.net> <4g2bi9$1fls@rs18.hrz.th-darmstadt.de> <19960220.7D62838.7EC3@mojaveg.ridgecrest.ca.us>
  11. NNTP-Posting-Host: anx1p5.cc.ruu.nl
  12. X-Newsreader: THOR 2.22 (Amiga TCP/IP)
  13.  
  14. >> #include <stdlib.h>
  15. >> ....
  16. >>    num = ( (float) rand() / 32767E0 ) * ( high - low ) + low;
  17.  
  18. >In theory, this is a very good suggestion.  In practice, rand() is a
  19. >very poor RNG.  Run any common statistical tests on rand()'s output
  20. >to see.
  21.  
  22. What's wrong with FastRand()?
  23.  
  24. From a totally unexpected corner I got this article, which, amongst a lot of
  25. stuff about why god doesn't exist, contained the following algorithm as an
  26. *example*, so I don't know how good it is. (layout slightly changed by me).
  27.  
  28. --
  29. From: PAGAN <pagan@delphi.com>
  30. Subject: Re: Atheism; the perfect antidote to poetry and style.
  31. Date: Sat, 10 Feb 96 04:15:01
  32. Newsgroups: alt.atheism.moderated
  33.  
  34. /*********************************************************************
  35.  RANDOM number generator
  36.      returns a random number from one to the value passed
  37.      repeat rate OTO 1e6
  38. *********************************************************************/
  39. int random(int val)
  40. { int num;                        /* this is what will be returned */
  41.   int bit0, bit1, bit14;
  42.   static int rndval=2;            /* seed value */
  43.  
  44.   if(val<0)                       /* if a negative value passed */
  45.       rndval=-val;                /* reseed */
  46.  
  47.   bit0 = rndval & 1;
  48.   bit1 = (rndval & 2) >> 1;
  49.   bit14 = bit0 ^ bit1;
  50.   rndval >>= 1;
  51.   rndval  |= bit14 << 14;
  52.  
  53.   num = rndval % val;
  54.   return num ? num : val;
  55. }
  56.  
  57. /*********************************************************************
  58.  SEED the RANDOM number generator
  59.      random number seed for the above
  60. *********************************************************************/
  61. seedrandom()               /* negative value to seed the generator */
  62. { random(-systemticks());  /* seed with system tick count */
  63. }
  64. --
  65.  
  66.  
  67. --
  68. Wessel Dankers                 _\\|//_            <wsldanke@cs.ruu.nl>
  69.                                ///|\\\
  70. ----------------------------oOO--(_)---OOo----------------------------
  71. `Never imagine yourself not to be otherwise than what it might appear
  72. to others that what you were or might have been was not otherwise than
  73. what you had been would have appeared to them to be otherwise.'
  74.  
  75.