home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / SourceCode / Classes / Neural-Network / Random.m < prev    next >
Encoding:
Text File  |  1991-05-30  |  3.7 KB  |  176 lines

  1. //
  2. // Random
  3. //
  4. // <<See the file Random.h for more information>>.
  5. //
  6. // Version 1.0, 1991 May 30
  7. //
  8. // Written by Gregor Purdy
  9. // gregor@oit.itd.umich.edu
  10. //
  11. // See the README and COPYING files included for
  12. // information and distribution and usage rights. 
  13. //
  14.  
  15.  
  16. #import <sys/time.h>
  17. #import "Random.h"
  18.  
  19.  
  20. @implementation Random
  21.  
  22. - init
  23. {
  24.     [super init];            // Make a new instance using superclass' method
  25.     [self newSeeds];            // Get a new seed for ourselves
  26.     
  27.     return self;
  28. }
  29.  
  30. - initSeeds:(int)s1 :(int)s2 :(int)s3
  31. {
  32.     [super init];
  33.     [self setSeeds:s1 :s2 :s3];
  34.  
  35.     return self;
  36. }
  37.  
  38. - newSeeds
  39. {
  40.     struct timeval theTime;        // gettimeofday return structure
  41.     
  42.     gettimeofday(&theTime,0);        // Get the time of day in seconds and microseconds
  43.     h1 = theTime.tv_usec;        // Set seed 1 by microseconds past second
  44.     gettimeofday(&theTime,0);        // Get the time of day in seconds and microseconds
  45.     h2 = theTime.tv_usec;        // Set seed 2 by microseconds past second
  46.     gettimeofday(&theTime,0);        // Get the time of day in seconds and microseconds
  47.     h3 = theTime.tv_usec;        // Set seed 3 by microseconds past second
  48.  
  49.     return self;    
  50. }
  51.  
  52. - setSeeds:(int) s1 :(int) s2 :(int) s3
  53. {
  54.     h1 = s1;                // Set the seeds to the values given
  55.     h2 = s2;
  56.     h3 = s3;
  57.     
  58.     return self;
  59. }
  60.  
  61. - getSeeds:(int *)s1 :(int *)s2 :(int *)s3
  62. {
  63.     if((s1 == NULL) || (s2 == NULL) || (s3 == NULL))
  64.     return nil;
  65.  
  66.     *s1 = h1;
  67.     *s2 = h2;
  68.     *s3 = h3;
  69.  
  70.     return self;
  71. }
  72.  
  73. //
  74. // See the Source article for the explanations of these constants
  75. //
  76. #define M1    32771
  77. #define M2    32779
  78. #define M3    32783
  79. #define F1    179
  80. #define F2    183
  81. #define F3    182
  82.  
  83. #define MAXNUM    32767
  84. #define RANGE    32768
  85.  
  86. - (int) rand
  87. {
  88.     h1 = (F1 * h1) % M1;            // Update the sections
  89.     h2 = (F2 * h2) % M2;
  90.     h3 = (F2 * h3) % M3;
  91.     
  92.     if ((h1 > MAXNUM) || (h2 > MAXNUM) || (h3 > MAXNUM))    // If a section is out of range,
  93.         return [self rand];            //   return next result
  94.     else                    // Otherwise,
  95.         return (h1 + h2 + h3) % RANGE;        //   Return this result
  96. }
  97.  
  98. - (int) randMax:(int)max
  99. {
  100.     return (int)((float)[self rand] / (float)RANGE * (float)(max + 1));
  101. }
  102.  
  103. - (int) randMin:(int)min max:(int)max
  104. {
  105.     return min + [self randMax:(max - min)];
  106. }
  107.  
  108. - (float) percent
  109. {
  110.     return ((float)[self rand] / (float)RANGE);
  111. }
  112.  
  113. - (int) rollDie:(int) numSides
  114. {
  115.     return [self randMax:(numSides - 1)] + 1;
  116. }
  117.  
  118. - (int) roll:(int) numRolls die:(int) numSides
  119. {
  120.     int temp = 0;
  121.     int loop;
  122.     
  123.     for (loop = 1 ; loop <= numRolls ; loop++ )
  124.     temp += [self rollDie:numSides];
  125.     
  126.     return temp;
  127. }
  128.  
  129. - (int) rollBest:(int)numWanted of:(int)numRolls die:(int)numSides
  130. {
  131.     int temp[numRolls];                // Array of rolls
  132.     int loop1;                    // First loop control variable
  133.     int loop2;                    // Second loop control variable
  134.     int highest;                // Index of highest found roll
  135.     int accumulator = 0;            // Accumulates total best roll
  136.     
  137.     for (loop1 = 1 ; loop1 <= numRolls ; loop1++)    // Fill an array with rolls
  138.     temp[loop1] = [self rollDie:numSides];
  139.     
  140.     for (loop1 = 1 ; loop1 <= numWanted; loop1++) {
  141.     highest = 1;                // Start off as if first is highest
  142.     for (loop2 = 2 ; loop2 <= numRolls ; loop2++)    // Scan array for higher rolls
  143.         if (temp[loop2] > temp[highest])    // If temp[loop2] is higher, then
  144.         highest = loop2;        //     remember that fact
  145.     accumulator += temp[highest];        // Add highest roll to accumulator
  146.     temp[highest] = 0;            // Clear highest roll so we don't find it again
  147.     }
  148.     
  149.     return accumulator;                // Return what we found
  150. }
  151.  
  152. - read:(NXTypedStream *)stream
  153. {
  154.     [super read:stream];
  155.     
  156.     NXReadTypes(stream, "iii", &h1, &h2, &h3);
  157.     
  158.     return self;
  159. }
  160.  
  161. - write:(NXTypedStream *)stream
  162. {
  163.     [super write:stream];
  164.     
  165.     NXWriteTypes(stream, "iii", &h1, &h2, &h3);
  166.  
  167.     return self;
  168. }
  169.  
  170.  
  171. @end
  172.  
  173.  
  174. //
  175. // End of file.
  176. //