SCAMP-5c SPI Interface  1.0.0
SCAMP-5c is connected to the flight computer via a SPI interface
jcRandom.hpp
1 
2 #ifndef JC_RANDOM_HPP
3 #define JC_RANDOM_HPP
4 
5 #include <cstdint>
6 #include <cstdlib>
7 
8 class jcRandom{
9 
10 protected:
11  uint32_t seed;
12  uint32_t m_w;// Marsaglia's MWC Algorithm
13  uint32_t m_z;
14  float *distribution;
15 
16  inline void roll(){
17  m_z = 36969*(m_z&65535) + (m_z>>16);
18  m_w = 18000*(m_w&65535) + (m_w>>16);
19  }
20 
21 public:
22  jcRandom();
23  jcRandom(uint32_t x);
24  ~jcRandom();
25 
26  void UseSeed(uint32_t x);
27  void UseTimeSeed(void);
28  void Reset(void);
29  void GenerateRandomMapping(uint32_t*dest,size_t dim);
30 
31  inline uint32_t GetSeed(){
32  return seed;
33  }
34 
35  inline uint32_t GetUint32(){
36  roll();
37  return (m_z<<16) + (m_w&0xFFFF);
38  }
39  inline uint16_t GetUint16(){
40  roll();
41  return (m_z<<8) + (m_w&0xFF);
42  }
43  inline double GetUniform(){
44  uint32_t u = this->GetUint32();
45  // The magic number below is 1/(2^32 + 2).
46  // The result is strictly between 0 and 1.
47  return (u + 1.0)*2.328306435454494e-10;
48  }
49 
50  inline int GetInt(int r_min,int r_max){
51  uint32_t s = r_max - r_min + 1;
52  uint32_t r = GetUint16();
53  return r*s/65536UL + r_min;
54  }
55  inline double GetDouble(double r_min,double r_max){
56  double r = GetUniform();
57  return (r*(r_max - r_min) + r_min);
58  }
59 
60  inline int operator()(int r_min,int r_max){
61  return GetInt(r_min,r_max);
62  }
63  inline double operator()(double r_min,double r_max){
64  return GetDouble(r_min,r_max);
65  }
66 
67  inline bool Rate(uint16_t Num,uint16_t Den){
68  uint32_t r = this->GetUint16();
69  return (Den*r)<(Num*65536UL);
70  }
71 
72 };
73 
74 #endif