home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / RAND1.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  8KB  |  242 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /************************************************************************
  4.  This random number generator originally appeared in "Toward a Universal
  5.  Random Number Generator" by George Marsaglia and Arif Zaman.
  6.  Florida State University Report: FSU-SCRI-87-50 (1987)
  7.  
  8.  It was later modified by F. James and published in "A Review of Pseudo-
  9.  random Number Generators"
  10.  
  11.  Converted from FORTRAN to C by Phil Linttell, James F. Hickling
  12.  Management Consultants Ltd, Aug. 14, 1989.
  13.  
  14.  THIS IS THE BEST KNOWN RANDOM NUMBER GENERATOR AVAILABLE.
  15.        (However, a newly discovered technique can yield
  16.          a period of 10^600. But that is still in the development stage.)
  17.  
  18.  It passes ALL of the tests for random number generators and has a period
  19.    of 2^144, is completely portable (gives bit identical results on all
  20.    machines with at least 24-bit mantissas in the floating point
  21.    representation).
  22.  
  23.  The algorithm is a combination of a Fibonacci sequence (with lags of 97
  24.    and 33, and operation "subtraction plus one, modulo one") and an
  25.    "arithmetic sequence" (using subtraction).
  26.  
  27.  On a Vax 11/780, this random number generator can produce a number in
  28.     13 microseconds.
  29. ************************************************************************/
  30.  
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <math.h>
  34. #include <time.h>
  35.  
  36. #define TRUE    1
  37. #define FALSE   0
  38.  
  39. float u[97], c, cd, cm;
  40. int i97, j97, test;
  41.  
  42. int rmarin(int ij, int kl);
  43. int ranmar(float rvec[], int len);
  44.  
  45.  
  46. int main()
  47. {
  48.  
  49.         float temp[100];
  50.         int i;
  51.         int ij, kl, len;
  52.  
  53.         /*These are the seeds needed to produce the test case results*/
  54.  
  55.         ij = 1802;
  56.         kl = 9373;
  57.  
  58.         /*Do the initialization*/
  59.  
  60.         if (1 == rmarin(ij,kl))
  61.                 return 1;
  62.  
  63.         /*Generate 20000 random numbers*/
  64.  
  65.         len = 100;
  66.         for ( i=0; i<=199 ; i++)
  67.                 if (1 == ranmar(temp, len))
  68.                         return 1;
  69.     
  70.         /*If the random number generator is working properly,
  71.           the next six random numbers should be:
  72.  
  73.             6533892.0  14220222.0   7275067.0
  74.             6172232.0   8354498.0  10633180.0
  75.         */
  76.  
  77.         len = 6;
  78.         if (1 == ranmar(temp, len))
  79.                 return 1;
  80.  
  81.         for ( i=0; i<=5; i++)
  82.                 printf("%12.1f\n",4096.0*4096.0*temp[i]);
  83.  
  84.         return 0;
  85. }
  86.  
  87.  
  88. /************************************************************************
  89.  This is the initialization routine for the random number generator RANMAR()
  90.  NOTE: The seed variables can have values between:    0 <= IJ <= 31328
  91.                                                       0 <= KL <= 30081
  92.  The random number sequences created by these two seeds are of sufficient
  93.  length to complete an entire calculation with. For example, if several
  94.  different groups are working on different parts of the same calculation,
  95.  each group could be assigned its own IJ seed. This would leave each group
  96.  with 30000 choices for the second seed. That is to say, this random
  97.  number generator can create 900 million different subsequences -- with
  98.  each subsequence having a length of approximately 10^30.
  99.  
  100.  Use IJ = 1802 & KL = 9373 to test the random number generator. The
  101.  subroutine RANMAR should be used to generate 20000 random numbers.
  102.  Then display the next six random numbers generated multiplied by 4096*4096
  103.  If the random number generator is working properly, the random numbers
  104.  should be:
  105.            6533892.0  14220222.0   7275067.0
  106.            6172232.0   8354498.0  10633180.0
  107. ************************************************************************/
  108.  
  109. int rmarin(int ij, int kl)
  110. {
  111.  
  112.         float s, t;
  113.         int i, j, k, l, m;
  114.         int ii, jj;
  115.  
  116.         /* Change FALSE to TRUE in the next statement to test the
  117.            random routine.*/
  118.  
  119.         test = TRUE;
  120.  
  121.         if ( ( ij < 0 || ij > 31328 ) ||
  122.                 ( kl < 0 || kl > 30081 ) )
  123.         {
  124.                 printf ("RMARIN: The first random number seed must have a "
  125.                         "value between 0 and 31328\n");
  126.                 printf ("        The second random number seed must have a "
  127.                         "value between 0 and 30081");
  128.                 return 1;
  129.         }
  130.  
  131.         i = (int)fmod(ij/177.0, 177.0) + 2;
  132.         j = (int)fmod(ij      , 177.0) + 2;
  133.         k = (int)fmod(kl/169.0, 178.0) + 1;
  134.         l = (int)fmod(kl      , 169.0);
  135.  
  136.         for ( ii=0; ii<=96; ii++ )
  137.         {
  138.                 s = (float)0.0;
  139.                 t = (float)0.5;
  140.                 for ( jj=0; jj<=23; jj++ )
  141.                 {
  142.                         m = (int)fmod( fmod(i*j,179.0)*k , 179.0 );
  143.                         i = j;
  144.                         j = k;
  145.                         k = m;
  146.                         l = (int)fmod( 53.0*l+1.0 , 169.0 );
  147.                         if ( fmod(l*m,64.0) >= 32)
  148.                                 s = s + t;
  149.                         t = (float)(0.5 * t);
  150.                 }
  151.                 u[ii] = s;
  152.         }
  153.  
  154.         c  = (float)(  362436.0 / 16777216.0);
  155.         cd = (float)( 7654321.0 / 16777216.0);
  156.         cm = (float)(16777213.0 / 16777216.0);
  157.  
  158.         i97 = 96;
  159.         j97 = 32;
  160.  
  161.         test = TRUE;
  162.  
  163.         return 0;
  164. }
  165.  
  166. int ranmar(float rvec[], int len)
  167. {
  168.         float uni;
  169.         int ivec;
  170.  
  171.         if ( !test )
  172.         {
  173.                 printf ("RANMAR: Call the initialization routine (RMARIN) "
  174.                         "before calling RANMAR.\n");
  175.                 return 1;
  176.         }
  177.  
  178.         for ( ivec=0; ivec < len; ivec++)
  179.         {
  180.                 uni = u[i97] - u[j97];
  181.                 if ( uni < 0.0F )
  182.                         uni = uni + 1.0;
  183.                 u[i97] = uni;
  184.                 i97--;
  185.                 if ( i97 < 0 )
  186.                         i97 = 96;
  187.                 j97--;
  188.                 if ( j97 < 0 )
  189.                         j97 = 96;
  190.                 c = c - cd;
  191.                 if ( c < 0.0F )
  192.                         c = c + cm;
  193.                 uni = uni - c;
  194.                 if ( uni < 0.0F )
  195.                         uni = uni + 1.0;
  196.                 rvec[ivec] = uni;
  197.         }
  198.         return 0;
  199. }
  200.  
  201. /* I use the following procedure in TC to generate seeds:
  202.  
  203.   The sow() procedure calculates two seeds for use with the random number
  204.   generator from the system clock.  I decided how to do this myself, and
  205.   I am sure that there must be better ways to select seeds; hopefully,
  206.   however, this is good enough.  The first seed is calculated from the values
  207.   for second, minute, hour, and year-day; weighted with the second most
  208.   significant and year-day least significant.  The second seed weights the
  209.   values in reverse.
  210. */
  211.  
  212. void sow( seed1, seed2 )
  213. int *seed1, *seed2;
  214. {
  215.         struct tm *tm_now;
  216.         float s_sig, s_insig, maxs_sig, maxs_insig;
  217.         time_t secs_now;
  218.         int s, m, h, d, s1, s2;
  219.  
  220.         time(&secs_now);
  221.         tm_now = localtime(&secs_now);
  222.  
  223.         s = tm_now->tm_sec + 1;
  224.         m = tm_now->tm_min + 1;
  225.         h = tm_now->tm_hour + 1;
  226.         d = tm_now->tm_yday + 1;
  227.  
  228.         maxs_sig   = (float)(60.0 + 60.0/60.0 + 24.0/60.0/60.0 +
  229.               366.0/24.0/60.0/60.0);
  230.         maxs_insig = (float)(60.0 + 60.0*60.0 + 24.0*60.0*60.0 +
  231.               366.0*24.0*60.0*60.0);
  232.  
  233.         s_sig      = (float)(s + m/60.0 + h/60.0/60.0 + d/24.0/60.0/60.0);
  234.         s_insig    = (float)(s + m*60.0 + h*60.0*60.0 + d*24.0*60.0*60.0);
  235.  
  236.         s1 = (int)(s_sig   / maxs_sig   * 31328.0);
  237.         s2 = (int)(s_insig / maxs_insig * 30081.0);
  238.  
  239.         *seed1 = s1;
  240.         *seed2 = s2;
  241. }
  242.