Advertisement
Guest User

xorshift.hpp

a guest
Mar 18th, 2012
795
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.79 KB | None | 0 0
  1. class xorshift {
  2.     // Simple shift-and-xor random number generator. This
  3.     // implementation provides an optimal period for its storage size,
  4.     // passes most empirical tests, and is faster and smaller than
  5.     // more popular approaches like MT.
  6.     //
  7.     // However, it is insecure. Don't use it as a source of
  8.     // cryptographic randomness.
  9.     //
  10.     // See www.jstatsoft.org/v08/i14/paper for the algorithm, and
  11.     // www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1452.html
  12.     // for the structure.
  13. public:
  14.     typedef std::uint32_t result_type;
  15.  
  16.     struct state_type {
  17.         result_type x;
  18.         result_type y;
  19.         result_type z;
  20.         result_type w;
  21.     };
  22.  
  23.     xorshift(void);
  24.     explicit xorshift(result_type r);
  25.     explicit xorshift(const state_type &seed);
  26.  
  27.     void seed(void);
  28.     void seed(result_type r);
  29.     void seed(const state_type &state);
  30.  
  31.     result_type operator()(void);
  32.         // Generate a uniformly-distributed random integer of
  33.         // result_type.
  34.  
  35.     void discard(unsigned long long z);
  36.         // Discard the next z random values.
  37.  
  38.     const state_type &state(void) const;
  39.     void state(const state_type &state);
  40.         // Get or set the entire state. This can be used to store
  41.         // and later re-load the state in any format.
  42.  
  43.     // Random number bounds. Used by random distributions; you
  44.     // probably don't need to call these directly.
  45.     static result_type min(void);
  46.     static result_type max(void);
  47.  
  48. private:
  49.     state_type state_;
  50. };
  51.  
  52. bool operator==(const xorshift &lhs, const xorshift &rhs);
  53. bool operator!=(const xorshift &lhs, const xorshift &rhs);
  54.     // Two engines compare as equal if their states are
  55.     // bitwise-identical, i.e. if they would generate the same
  56.     // numbers forever.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement