YAKL
YAKL_random.h
Go to the documentation of this file.
1 
2 #pragma once
3 // Included by YAKL.h
4 
6 namespace yakl {
7 
8  // Simple, low-overhead random numbers
9  // https://burtleburtle.net/bob/rand/smallprng.html
10 
21  class Random {
22  protected:
24  typedef unsigned long long u8;
26  u8 static constexpr rot(u8 x, u8 k) { return (((x)<<(k))|((x)>>(64-(k)))); }
28  struct State { u8 a, b, c, d; };
30  State state;
31 
32  public:
33 
35  YAKL_INLINE Random() { set_seed(1368976481L); } // I made up this number
37  YAKL_INLINE Random(u8 seed) { set_seed(seed); }
39  YAKL_INLINE Random(Random const &in) { this->state = in.state; }
41  YAKL_INLINE Random(Random &&in) { this->state = in.state; }
43  YAKL_INLINE Random &operator=(Random const &in) { this->state = in.state; return *this; }
45  YAKL_INLINE Random &operator=(Random &&in) { this->state = in.state; return *this; }
46 
48  YAKL_INLINE void set_seed(u8 seed) {
49  state.a = 0xf1ea5eed; state.b = seed; state.c = seed; state.d = seed;
50  for (int i=0; i<20; ++i) { gen(); }
51  }
52 
55  u8 e = state.a - rot(state.b, 7);
56  state.a = state.b ^ rot(state.c,13);
57  state.b = state.c + rot(state.d,37);
58  state.c = state.d + e;
59  state.d = e + state.a;
60  return state.d;
61  }
62 
65  template <class T> YAKL_INLINE T genFP() {
66  return static_cast<T>(gen()) / static_cast<T>(std::numeric_limits<u8>::max());
67  }
68 
73  template <class T> YAKL_INLINE T genFP(T lb, T ub) {
74  return genFP<T>() * (ub-lb) + lb;
75  }
76 
77  };
78 
79 }
81 
82 
yakl::Random::operator=
YAKL_INLINE Random & operator=(Random const &in)
Copies a Random object.
Definition: YAKL_random.h:43
yakl::Random::genFP
YAKL_INLINE T genFP()
Generates a random floating point value between 0 and 1
Definition: YAKL_random.h:65
yakl::Random
Non-cryptographic pseudo-random number generator with a very small internal state.
Definition: YAKL_random.h:21
yakl::Random::gen
YAKL_INLINE u8 gen()
Generates a random unsigned integer between zero and std::numeric_limits<u8>::max() - 1
Definition: YAKL_random.h:54
__YAKL_NAMESPACE_WRAPPER_END__
#define __YAKL_NAMESPACE_WRAPPER_END__
Definition: YAKL.h:20
yakl::Random::Random
YAKL_INLINE Random(Random const &in)
Copies a Random object.
Definition: YAKL_random.h:39
__YAKL_NAMESPACE_WRAPPER_BEGIN__
#define __YAKL_NAMESPACE_WRAPPER_BEGIN__
Definition: YAKL.h:19
YAKL_INLINE
#define YAKL_INLINE
Used to decorate functions called from kernels (parallel_for and parallel_outer) or from CPU function...
Definition: YAKL_defines.h:140
yakl::Random::Random
YAKL_INLINE Random(Random &&in)
Moves a Random object.
Definition: YAKL_random.h:41
yakl::Random::operator=
YAKL_INLINE Random & operator=(Random &&in)
Moves a Random object.
Definition: YAKL_random.h:45
yakl::Random::genFP
YAKL_INLINE T genFP(T lb, T ub)
Generates a random floating point value between lb and ub
Definition: YAKL_random.h:73
yakl::Random::set_seed
YAKL_INLINE void set_seed(u8 seed)
Assigns a seed. Warm-up of 20 iterations.
Definition: YAKL_random.h:48
yakl::Random::Random
YAKL_INLINE Random(u8 seed)
Initializes a prng object with the specified seed. Warm-up of 20 iterations.
Definition: YAKL_random.h:37
yakl::Random::Random
YAKL_INLINE Random()
Initializes a prng object with the seed 1368976481. Warm-up of 20 iterations.
Definition: YAKL_random.h:35
yakl