home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / xplatfrm / tierra / trand.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-26  |  2.7 KB  |  92 lines

  1. /* trand.c  19-8-91  adapted from ``Numerical Recipes in C'' by Press,
  2. Flannery, Teukolsky and Vetterling.  Tdrand() returns a uniform random deviate
  3. between 0.0 and 1.0.  Tlrand() returns a uniform random deviate between 0 and
  4. 2^31 */
  5.  
  6. #ifndef lint
  7. static char     sccsid[] = "%W%     %G%";
  8. #endif
  9.  
  10. #include "tierra.h"
  11. #include "extern.h"
  12.  
  13. I32s tlrand() /* returns random 32 bit positive signed integer */
  14. {   return (I32s) (tdrand() * (double) LONG_MAX);
  15. }
  16.  
  17. I16s tirand() /* returns random 16 bit positive signed integer */
  18. {   return (I16s) (tdrand() * (double) INT_MAX);
  19. }
  20.  
  21. I16u tuirand() /* returns random 16 bit unsigned integer */
  22. {   return (I16u) (tdrand() * (double) UINT_MAX);
  23. }
  24.  
  25. I8s tcrand() /* returns random 8 bit positive signed integer */
  26. {   return (I8s) (tdrand() * (double) CHAR_MAX);
  27. }
  28.  
  29. I8u tucrand() /* returns random 8 bit unsigned integer */
  30. {   return (I8u) (tdrand() * (double) UCHAR_MAX);
  31. }
  32.  
  33. #define M1 259200L
  34. #define IA1 7141L
  35. #define IC1 54773L
  36. #define RM1 (1.0/M1)
  37. #define M2 134456L
  38. #define IA2 8121L
  39. #define IC2 28411L
  40. #define RM2 (1.0/M2)
  41. #define M3 243000L
  42. #define IA3 4561L
  43. #define IC3 51349L
  44.  
  45. void tsrand(seed)
  46. I32s  seed;
  47. {   I16s  j;
  48.  
  49.     RandIx1 = (IC1 + seed) % M1;       /* Seed the first routine */
  50.     RandIx1 = (IA1 * RandIx1 + IC1) % M1;
  51.     RandIx2 = RandIx1 % M2;            /* and use it to seed the second */
  52.     RandIx1 = (IA1 * RandIx1 + IC1) % M1;
  53.     RandIx3 = RandIx1 % M3;            /* and third routines. */
  54.     for (j = 1; j <= 97; j++)    /* Fill the table with sequential uniform */
  55.     {   RandIx1 = (IA1 * RandIx1 + IC1) % M1; /* deviates generated by the */
  56.         RandIx2 = (IA2 * RandIx2 + IC2) % M2; /* first two routines. */
  57.         TrandArray[j] = (RandIx1 + RandIx2 * RM2) * RM1;
  58.     }                    /* Low- & high-order pieces combined here */
  59. }
  60.  
  61. double tdrand()
  62. {   double temp;
  63.     I16s j;
  64.  
  65.     RandIx1 = (IA1 * RandIx1 + IC1) % M1;
  66.                              /* Except when initializing, this is where we */
  67.     RandIx2 = (IA2 * RandIx2 + IC2) % M2;
  68.                              /* start.  Generate the next number for each */
  69.     RandIx3 = (IA3 * RandIx3 + IC3) % M3;                  /* sequence. */
  70.     j = 1 + ((97 * RandIx3) / M3);
  71.                        /* Use the third sequence to get an integer between */
  72.     if (j > 97 || j < 1) /* 1 and 97 */
  73.     {   sprintf(mes[0],"RAN1: This cannot happen.");
  74.         FEMessage(1);
  75.     }
  76.     temp = TrandArray[j];               /* Return that table entry, */
  77.     TrandArray[j] = (RandIx1 + RandIx2 * RM2) * RM1;  /* and refill it. */
  78.     return temp;
  79. }
  80.  
  81. #undef M1
  82. #undef IA1
  83. #undef IC1
  84. #undef RM1
  85. #undef M2
  86. #undef IA2
  87. #undef IC2
  88. #undef RM2
  89. #undef M3
  90. #undef IA3
  91. #undef IC3
  92.