Advertisement
Guest User

xorshift.cpp

a guest
Mar 18th, 2012
983
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.92 KB | None | 0 0
  1. #include "xorshift.hpp"
  2.  
  3. static const xorshift::state_type s_default_seed = {
  4.     123456789, 362436069, 521288629, 88675123
  5. };
  6.  
  7. xorshift::xorshift(void)
  8.     : state_(s_default_seed)
  9. {}
  10.  
  11. xorshift::xorshift(const state_type &seed)
  12.     : state_(seed)
  13. {}
  14.  
  15. xorshift::xorshift(result_type r)
  16.     : state_({s_default_seed.x, s_default_seed.y, s_default_seed.z, r})
  17. {}
  18.  
  19. void xorshift::seed(const state_type &seed) {
  20.     state(seed);
  21. }
  22.  
  23. void xorshift::seed(void) {
  24.     state(s_default_seed);
  25. }
  26.  
  27. void xorshift::seed(result_type r) {
  28.     auto seed = s_default_seed;
  29.     seed.w = r;
  30.     state(seed);
  31. }
  32.  
  33. void xorshift::discard(unsigned long long z) {
  34.     while (z--)
  35.         (*this)();
  36. }
  37.  
  38. const xorshift::state_type &xorshift::state(void) const {
  39.     return state_;
  40. }
  41.  
  42. void xorshift::state(const state_type &state) {
  43.     state_ = state;
  44. }
  45.  
  46. xorshift::result_type xorshift::min(void) {
  47.     return std::numeric_limits<result_type>::min();
  48. }
  49.  
  50. xorshift::result_type xorshift::max(void) {
  51.     return std::numeric_limits<result_type>::max();
  52. }
  53.  
  54. xorshift::result_type xorshift::operator()(void) {
  55.     result_type t = state_.x ^ (state_.x << 15);
  56.     state_.x = state_.y; state_.y = state_.z; state_.z = state_.w;
  57.     return state_.w = state_.w ^ (state_.w >> 21) ^ (t ^ (t >> 4));
  58. }
  59.  
  60. static bool operator==(
  61.     const xorshift::state_type &lhs, const xorshift::state_type &rhs)
  62. {
  63.     return lhs.x == rhs.x
  64.         && lhs.y == rhs.y
  65.         && lhs.z == rhs.z
  66.         && lhs.w == rhs.w;
  67. }
  68.  
  69. static bool operator!=(
  70.     const xorshift::state_type &lhs, const xorshift::state_type &rhs)
  71. {
  72.     return lhs.x != rhs.x
  73.         || lhs.y != rhs.y
  74.         || lhs.z != rhs.z
  75.         || lhs.w != rhs.w;
  76. }
  77.  
  78. bool operator==(const xorshift &lhs, const xorshift &rhs) {
  79.     return lhs.state() == rhs.state();
  80. }
  81.  
  82. bool operator!=(const xorshift &lhs, const xorshift &rhs) {
  83.     return lhs.state() != rhs.state();
  84. }
  85.  
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement