home *** CD-ROM | disk | FTP | other *** search
/ PC Format (South-Africa) 2001 June / PCFJune.iso / Xenon / XenonSource.exe / gamesystem / source / gs_random.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-01  |  1.7 KB  |  84 lines

  1. //-------------------------------------------------------------
  2. //
  3. // Class:    gsCRandom
  4. //
  5. // Author:    John M Phillips
  6. //
  7. // Started:    12/03/00
  8. //
  9. // Base:    gsCObject
  10. //
  11. // Derived:    None
  12. //
  13. //-------------------------------------------------------------
  14.  
  15. #include "gamesystem.h"
  16.  
  17. //-------------------------------------------------------------
  18.  
  19. gsCRandom::gsCRandom()
  20. {
  21.     m_seed = gsRANDOM_DEFAULT_SEED & 0xFFFF;
  22. }
  23.  
  24. //-------------------------------------------------------------
  25.  
  26. gsCRandom::~gsCRandom()
  27. {
  28. }
  29.  
  30. //-------------------------------------------------------------
  31.  
  32. void gsCRandom::next()
  33. {
  34.     m_seed = ((m_seed * 43) + 1509) & 0xFFFF;
  35. }
  36.  
  37. //-------------------------------------------------------------
  38.  
  39. void gsCRandom::setSeed(int seed)
  40. {
  41.     m_seed = seed & 0xFFFF;
  42. }
  43.  
  44. //-------------------------------------------------------------
  45.  
  46. float gsCRandom::getFloat(float upper_bound)
  47. {
  48.     next();
  49.     return upper_bound * (float) m_seed / 65535.f;
  50. }
  51.  
  52. //-------------------------------------------------------------
  53.  
  54. float gsCRandom::getFloat(float lower_bound,float upper_bound)
  55. {
  56.     return lower_bound + getFloat(upper_bound - lower_bound);
  57. }
  58.  
  59. //-------------------------------------------------------------
  60.  
  61. int gsCRandom::getInt(int upper_bound)
  62. {
  63.     next();
  64.     return upper_bound * m_seed / 65535;
  65. }
  66.  
  67. //-------------------------------------------------------------
  68.  
  69. int gsCRandom::getInt(int lower_bound,int upper_bound)
  70. {
  71.     next();
  72.     return lower_bound + getInt(upper_bound - lower_bound);
  73. }
  74.  
  75. //-------------------------------------------------------------
  76.  
  77. bool gsCRandom::getBool()
  78. {
  79.     next();
  80.     return m_seed & 1;
  81. }
  82.  
  83. //-------------------------------------------------------------
  84.