Euphoria
random.h
Go to the documentation of this file.
1 #pragma once
2 
3 
4 #include "range/v3/view/span.hpp"
5 
6 #include "assert/assert.h"
7 
8 #include "base/ints.h"
9 #include "base/vec2.h"
10 #include "base/range.h"
11 
12 
13 namespace eu
14 {
15  // todo(Gustav): make all rng use the same simple api
16  // and template the helper functions with user user facing non-templated functions can pick the rng that is needed
17 
20  struct Random
21  {
23  U32 state[16];
24 
25  explicit Random(U32 seed = generate_time_seed());
26 
27  U32 get_next_u32();
28  U64 get_next_u64();
29  float get_next_float01();
30  bool get_next_bool();
31  int get_next_sign();
32 
33  [[nodiscard]] static U32 generate_time_seed();
34  };
35 
36  /*
37  All helper functions should be in the style of
38 
39  ```
40  xxx get_random_xxx(random* rand, ...);
41  ```
42  */
43 
44  // move to point class or a circle class?
46 
48 
49  template <typename T>
50  T get_random_in_range(Random* rand, const Range<T>& range)
51  {
52  return from_01(range, rand->get_next_float01());
53  }
54 
55  template <typename T>
57  {
58  return static_cast<T>(min + rand->get_next_float01() * (max - min));
59  }
60 
61  template <typename T>
63  {
64  return get_random_in_range<T>(rand, 0, max);
65  }
66 
67  template <typename T>
68  const T& get_random_item_in_vector(Random* r, const std::vector<T>& v)
69  {
70  const auto size = v.size();
71  ASSERT(size > 0);
72  if (size == 1)
73  {
74  return v[0];
75  }
76  return v[get_random_in_range(r, size)];
77  }
78 
79  template <typename T>
80  const T& get_random_item_in_vector(Random* r, const ranges::span<T>& v)
81  {
82  const auto size = v.size();
83  ASSERT(size > 0);
84  if (size == 1)
85  {
86  return v[0];
87  }
88  return v[get_random_in_range(r, size)];
89  }
90 
91  float get_random_gaussian_float01(Random* rand);
92  float get_random_gaussian(Random* rand, float mean, float std_dev);
93  float get_random_gaussian(Random* rand, float mean, float std_dev, const Range<float>& r);
94 }
95 
#define ASSERT(x)
Definition: assert.h:29
Definition: assert.h:90
float get_random_gaussian_float01(Random *rand)
Definition: random.cc:84
std::uint32_t U32
Definition: ints.h:13
T get_random_in_range(Random *rand, const Range< T > &range)
Definition: random.h:50
const T & get_random_item_in_vector(Random *r, const std::vector< T > &v)
Definition: random.h:68
vec2f get_random_point_on_unit_circle_center_focused(Random *r)
Definition: random.cc:133
float from_01(const Range< float > &range, float value)
Definition: range.cc:13
size2f min(const size2f lhs, const size2f rhs)
Definition: size2.cc:140
std::uint64_t U64
Definition: ints.h:12
size2f max(const size2f lhs, const size2f rhs)
Definition: size2.cc:149
float get_random_gaussian(Random *rand, float mean, float std_dev)
Definition: random.cc:104
vec2f get_random_point_on_unit_circle_uniform(Random *r)
Definition: random.cc:142
WEL512 Random Number Generator.
Definition: random.h:21
U32 index
Definition: random.h:22
float get_next_float01()
Definition: random.cc:78
bool get_next_bool()
Definition: random.cc:121
U32 get_next_u32()
Definition: random.cc:54
U64 get_next_u64()
Definition: random.cc:70
int get_next_sign()
Definition: random.cc:127
Random(U32 seed=generate_time_seed())
Definition: random.cc:44
static U32 generate_time_seed()
Definition: random.cc:28
U32 state[16]
Definition: random.h:23
Definition: vec2.h:33