DLPrimitives
random.hpp
1 #pragma once
2 #include <cstdint>
3 #include <array>
4 #include <time.h>
5 
6 namespace dlprim {
7 
10  namespace philox {
11 
12  constexpr int result_items = 4;
13 
15  typedef std::array<std::uint32_t,result_items> uint_result_type;
17  typedef std::array<float,result_items> float_result_type;
18 
22  float_result_type calculate_float(std::uint64_t seed, std::uint64_t seq);
26  uint_result_type calculate_integer(std::uint64_t seed, std::uint64_t seq);
27 
28  };
29 
30  class RandomState {
31  public:
32  typedef std::uint64_t seed_type;
33  typedef std::uint64_t sequence_type;
34  RandomState(std::uint64_t seed)
35  {
36  seed_ = seed;
37  sequence_ = 0;
38  }
39  RandomState()
40  {
41  seed_ = time(nullptr) + 0xDEADBEEF;
42  }
43  std::uint64_t sequence_bump(size_t items)
44  {
45  std::uint64_t cur = sequence_;
46  sequence_ += items;
47  return cur;
48  }
49  void seed(std::uint64_t s)
50  {
51  seed_ = s;
52  }
53  std::uint64_t seed() const
54  {
55  return seed_;
56  }
57 
58  private:
59 
60  std::uint64_t sequence_;
61  std::uint64_t seed_;
62  };
63 
64 
65 
66 } // dlprim
uint_result_type calculate_integer(std::uint64_t seed, std::uint64_t seq)
Get uint32 result in range[0::UINT_MAX].
float_result_type calculate_float(std::uint64_t seed, std::uint64_t seq)
Get float result in range[0.0,1.0f)
std::array< float, result_items > float_result_type
Each round returns 4 items - in range[0.0,1.0f)
Definition: random.hpp:17
std::array< std::uint32_t, result_items > uint_result_type
Each round returns 4 items - result.
Definition: random.hpp:15
Mane namespace.
Definition: context.hpp:9
Definition: random.hpp:30