Luzhiled's Library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub ei1333/library

:warning: Xor-Shift
(other/xor-shift.hpp)

概要

Xorshift は擬似乱数生成法の一つである.

周期が $2^{64}-1$ となる実装を示した.

Code

/**
 * @brief Xor-Shift
 * @docs docs/xor-shift.md
 */
struct XorShift {
private:
  constexpr static double R = 1.0 / 0xffffffff;
  uint64_t x;

public:
  explicit XorShift(uint64_t seed = 88172645463325252ull) : x(seed) {}

  template< typename T = uint64_t >
  inline T get() { // [0, 2^64)
    x ^= x << 7ull;
    x ^= x >> 9ull;
    return x;
  }

  inline uint32_t get(uint32_t r) { // [0, r)
    return ((uint64_t) get< uint32_t >() * r) >> 32ull;
  }

  inline uint32_t get(uint32_t l, uint32_t r) { // [l, r)
    return l + get(r - l);
  }

  inline double probability() { // [0.0, 1.0]
    return get< uint32_t >() * R;
  }
};
#line 1 "other/xor-shift.hpp"
/**
 * @brief Xor-Shift
 * @docs docs/xor-shift.md
 */
struct XorShift {
private:
  constexpr static double R = 1.0 / 0xffffffff;
  uint64_t x;

public:
  explicit XorShift(uint64_t seed = 88172645463325252ull) : x(seed) {}

  template< typename T = uint64_t >
  inline T get() { // [0, 2^64)
    x ^= x << 7ull;
    x ^= x >> 9ull;
    return x;
  }

  inline uint32_t get(uint32_t r) { // [0, r)
    return ((uint64_t) get< uint32_t >() * r) >> 32ull;
  }

  inline uint32_t get(uint32_t l, uint32_t r) { // [l, r)
    return l + get(r - l);
  }

  inline double probability() { // [0.0, 1.0]
    return get< uint32_t >() * R;
  }
};
Back to top page