home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / g__inc / xrng.h < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-23  |  2.3 KB  |  84 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1988 Free Software Foundation
  4.     written by Dirk Grunwald (grunwald@cs.uiuc.edu)
  5.  
  6. This file is part of the GNU C++ Library.  This library is free
  7. software; you can redistribute it and/or modify it under the terms of
  8. the GNU Library General Public License as published by the Free
  9. Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.  This library is distributed in the hope
  11. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  12. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  13. PURPOSE.  See the GNU Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with this library; if not, write to the Free Software
  16. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #ifndef _RNG_h
  19. #define _RNG_h 1
  20. #ifdef __GNUG__
  21. #pragma once
  22. #pragma interface
  23. #endif
  24.  
  25. #include <assert.h>
  26. #include <math.h>
  27.  
  28. union PrivateRNGSingleType {               // used to access floats as unsigneds
  29.     float s;
  30.     unsigned long u;
  31. };
  32.  
  33. union PrivateRNGDoubleType {               // used to access doubles as unsigneds
  34.     double d;
  35.     unsigned long u[2];
  36. };
  37.  
  38. //
  39. // Base class for Random Number Generators. See ACG and MLCG for instances.
  40. //
  41. class RNG {
  42.     static PrivateRNGSingleType singleMantissa;    // mantissa bit vector
  43.     static PrivateRNGDoubleType doubleMantissa;    // mantissa bit vector
  44. public:
  45.     RNG();
  46.     //
  47.     // Return a long-words word of random bits
  48.     //
  49.     virtual unsigned long asLong() = 0;
  50.     virtual void reset() = 0;
  51.     //
  52.     // Return random bits converted to either a float or a double
  53.     //
  54.     float asFloat();
  55.     double asDouble();
  56. };
  57.  
  58. #if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
  59.  
  60.  
  61. inline float RNG::asFloat()
  62. {
  63.     PrivateRNGSingleType result;
  64.     result.s = 1.0;
  65.     result.u |= (asLong() & singleMantissa.u);
  66.     result.s -= 1.0;
  67.     assert( result.s < 1.0 && result.s >= 0);
  68.     return( result.s );
  69. }
  70.     
  71. inline double RNG::asDouble()
  72. {
  73.     PrivateRNGDoubleType result;
  74.     result.d = 1.0;
  75.     result.u[0] |= (asLong() & doubleMantissa.u[0]);
  76.     result.u[1] |= (asLong() & doubleMantissa.u[1]);
  77.     result.d -= 1.0;
  78.     assert( result.d < 1.0 && result.d >= 0);
  79.     return( result.d );
  80. }
  81.  
  82. #endif
  83. #endif
  84.