home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 (1993) / nebula.bin / SourceCode / Random / Random.h < prev    next >
Encoding:
Text File  |  1993-01-19  |  3.3 KB  |  123 lines

  1. //
  2. // Random
  3. //
  4. // An Objective-C class for NeXT computers to provide services for random
  5. // number generation and also die rolling.
  6. //
  7. // The Random class implements its own random number generator with a cycle
  8. // length of 8.8 trillion.
  9. // 
  10. // Upon creation of a Random, the seeds are set using the system clock.
  11. // Three calls are made to the system clock function, and for each the 
  12. // microseconds are used as the seed value. Thus, the relationships between
  13. // the seeds are dependant upon system load.
  14. //
  15. // The algorithm used by the Random class is that given in the article:
  16. //   "A Higly Random Random-Number Generator" by T.A. Elkins
  17. //   Computer Language, Volume 6, Number 12 (December 1989), Pages 59-65
  18. //   Published by:
  19. //        Miller Freeman Publications
  20. //        500 Howard Street
  21. //        San Francisco, CA  94105
  22. //        (415) 397-1881
  23. //
  24. // History:
  25. //   pre 1990 Mar 23
  26. //     Used random number generation algorithm from K&R.
  27. //   1990 Mar 23
  28. //     Modified to use algorithm with cycle length of 8.8 trillion.
  29. //   1990 Mar 26
  30. //     * Added archiving.
  31. //     * Added randMax:, randMin:Max:, and percent:.
  32. //   1991 Apr 26
  33. //     * Changed to use +alloc and -init as all NeXTStep 2.0 objects should.
  34. //   1991 May 30
  35. //     * Prepared for distribution and initial release.
  36. //
  37. //   1991 Nov 5
  38. //     * Added - (BOOL)bool method.
  39. //
  40. //   1991 Dec 30
  41. //     * Changed - (float)percent to return double instead.
  42. //     * Added - (double)randFunc: method.
  43. //
  44. //   1992 Feb 27
  45. //     * Added Gaussian functionality.
  46. //
  47. // Version 1.1, 1992 Feb 27 
  48. //
  49. // Written by Gregor Purdy
  50. // gregor@umich.edu
  51. //
  52. // See the README file included for information
  53. // and distribution and usage rights. 
  54. //
  55.  
  56.  
  57. #import <objc/Object.h>
  58. #import <objc/typedstream.h>
  59.  
  60.  
  61. typedef double (*ddfunc)(double);            // Double Function Returning Double.
  62.  
  63.  
  64. @interface Random : Object
  65.  
  66.  
  67. {
  68.     int h1, h2, h3;            // Seeds.
  69.     
  70.     int        iset;            // For gaussian generation.
  71.     double    gset;
  72.     
  73.     double    gscale;            // Gaussian scaling;
  74.     double    gorigin;        // Gaussian origin;
  75. }
  76.  
  77. + (int)version;                // Version of the class.
  78.  
  79. - init;                    // Init with seeds from newSeeds.
  80. - initSeeds:(int)s1            // Init with seeds given.
  81.   :(int)s2
  82.   :(int)s3;
  83.  
  84. - newSeeds;                // Get seeds from system time.
  85. - setSeeds:(int) s1            // Set seeds to those given.
  86.   :(int) s2
  87.   :(int) s3;
  88. - getSeeds:(int *)s1            // Put the seeds into some vars.
  89.   :(int *)s2
  90.   :(int *)s3;
  91.  
  92. - (int)rand;                // Return a random integer.
  93. - (int)randMax:(int)max;        // Return a random integer 0 <= x <= max.
  94. - (int)randMin:(int)min            // Return a random integer min <= x <= max.
  95.   max:(int)max;
  96. - (double)percent;            // Return a random double 0.0 <= x <= 1.0.
  97. - (BOOL)bool;                // Return randomly, YES or NO.
  98.  
  99. - (int)rollDie:(int)numSides;        // Return a random integer 1 <= x <= numSides.
  100. - (int)roll:(int)numRolls        // Return the best numWanted of numRolls rolls.
  101.   die:(int)numSides;
  102. - (int)rollBest:(int)numWanted        // Return integer sum of best numWanted rolls.
  103.   of:(int)numRolls
  104.   die:(int)numSides;
  105.  
  106. - (double)randFunc:(ddfunc)func;    // See description file.
  107.  
  108. - (double)gScale;
  109. - setGScale:(double)aScale;
  110. - (double)gOrigin;
  111. - setGOrigin:(double)anOrigin;
  112. - (double)gaussian;            // Return gausian variable.
  113.  
  114. - read:(NXTypedStream *)stream;        // De-archive from a typed stream.
  115. - write:(NXTypedStream *)stream;    // Archive to a typed stream.
  116.  
  117.  
  118. @end
  119.  
  120.  
  121. //
  122. // End of file.
  123. //