ionRand.h
Go to the documentation of this file.
1 
9 #pragma once
10 
11 #include <ionConfig.h>
12 #include "ionTypes.h"
13 
14 #include <cstdlib>
15 
16 
19 static inline float frand()
20 {
21  return std::rand() / (float) RAND_MAX;
22 }
23 
26 static inline float nrand()
27 {
28  return (std::rand() / (float) RAND_MAX) * 2.f - 1.f;
29 }
30 
33 class Random
34 {
35 
36 public:
37 
38  static float Between(float const Min, float const Max)
39  {
40  return frand() * (Max - Min) + Min;
41  }
42 
43  static int Between(int const Min, int const Max)
44  {
45  return std::rand() % (Max - Min) + Min;
46  }
47 
48 };
49 
53 {
54 
55 public:
56 
57  virtual u32 const Generate() = 0;
58  virtual void Seed(u32 const seed) = 0;
59 
60  u32 const Generate(u32 const max)
61  {
62  return Generate() % max;
63  }
64 
65  u32 const Generate(u32 const min, u32 const max)
66  {
67  return Generate() % (max - min) + min;
68  }
69 
70  f32 const Generatef32(u32 const modulus = 100000)
71  {
72  return (Generate() % modulus) / (f32) modulus;
73  }
74 
75  f32 const Generatef32(f32 const max, u32 const modulus = 100000)
76  {
77  return Generatef32(modulus) * max;
78  }
79 
80  f32 const Generatef32(f32 const min, f32 const max, u32 const modulus = 100000)
81  {
82  return Generatef32(modulus) * (max - min) + min;
83  }
84 
85  f64 const Generatef64(u32 const modulus = 100000)
86  {
87  return (Generate() % modulus) / (f64) modulus;
88  }
89 
90  f64 const Generatef64(f64 const max, u32 const modulus = 100000)
91  {
92  return Generatef64(modulus) * max;
93  }
94 
95  f64 const Generatef64(f64 const min, f64 const max, u32 const modulus = 100000)
96  {
97  return Generatef64(modulus) * (max - min) + min;
98  }
99 
100 };
101 
105 {
106 
107  u32 Value;
108 
109 public:
110 
112  {
113  Seed(seed);
114  }
115 
116  u32 const Generate()
117  {
118  return (Value = (Value * 214013 + 2531011) & ((1U << 31) - 1));
119  }
120 
121  void Seed(u32 const seed)
122  {
123  Value = seed;
124  }
125 
126 };
u32 const Generate(u32 const max)
Definition: ionRand.h:60
f64 const Generatef64(f64 const max, u32 const modulus=100000)
Definition: ionRand.h:90
static int Between(int const Min, int const Max)
Definition: ionRand.h:43
u32 const Generate(u32 const min, u32 const max)
Definition: ionRand.h:65
f32 const Generatef32(f32 const min, f32 const max, u32 const modulus=100000)
Definition: ionRand.h:80
u32 const Generate()
Definition: ionRand.h:116
CLinearCongruentialGenerator(u32 const seed=0)
Definition: ionRand.h:111
Interface for random number generation.
Definition: ionRand.h:52
Helper methods for generating random numbers.
Definition: ionRand.h:33
float f32
Definition: ionTypes.h:94
f32 const Generatef32(f32 const max, u32 const modulus=100000)
Definition: ionRand.h:75
Uint32 u32
Definition: ionTypes.h:89
f32 const Generatef32(u32 const modulus=100000)
Definition: ionRand.h:70
void Seed(u32 const seed)
Definition: ionRand.h:121
Defines typedefs for integer and floating point types.
Basic LCG implementation.
Definition: ionRand.h:104
double f64
Definition: ionTypes.h:95
static float Between(float const Min, float const Max)
Definition: ionRand.h:38
f64 const Generatef64(f64 const min, f64 const max, u32 const modulus=100000)
Definition: ionRand.h:95
f64 const Generatef64(u32 const modulus=100000)
Definition: ionRand.h:85