MFMv2.0.10
Movable Feast Machine Simulator 2.0.10
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Random.h
Go to the documentation of this file.
1 /* -*- mode:C++ -*-
2  Random.h PRNG interface for the Mersenne Twister
3  Copyright (C) 2014 The Regents of the University of New Mexico. All rights reserved.
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
28 #ifndef _RANDOM_H_
29 #define _RANDOM_H_
30 
31 #include <stdlib.h>
32 
33 #include "itype.h"
34 #include "RandMT.h"
35 #include "BitVector.h"
36 #include "FXP.h"
37 #include "Fail.h"
38 
39 namespace MFM
40 {
41 
45  class Random
46  {
47  public:
48 
53  { }
54 
59  Random(u32 seed)
60  {
61  SetSeed(seed);
62  }
63 
69  u32 Create() ;
70 
79  u32 Create(u32 max) ;
80 
86  bool CreateBool()
87  {
88  return OneIn(2);
89  }
90 
96  bool OneIn(u32 odds) { return OddsOf(1,odds); }
97 
105  bool OddsOf(u32 thisMany, u32 outOfThisMany) ;
106 
114  template <int P>
115  bool OddsOf(FXP<P> thisMany, FXP<P> outOfThisMany)
116  {
117  if (outOfThisMany <= 0)
118  {
119  FAIL(ILLEGAL_ARGUMENT);
120  }
121 
122  if (thisMany <= 0)
123  {
124  return false;
125  }
126  if (thisMany >= outOfThisMany)
127  {
128  return true;
129  }
130 
131  return OddsOf(thisMany.intValue, outOfThisMany.intValue);
132  }
133 
140  s32 Between(s32 min, s32 max) ;
141 
147  void SetSeed(u32 seed)
148  {
149  _generator.seedMT_MFM(seed);
150  }
151 
152  private:
153  RandMT _generator;
154 
155  };
156 
157  /******************************************************************************
158  ** PUBLIC INLINE FUNCTIONS **
159  ******************************************************************************/
160 
161  inline u32 Random::Create()
162  {
163  return _generator.randomMT();
164  }
165 
166  // Avoid modulus artifacts by sampling from round powers of 2 and rejecting
167  inline u32 Random::Create(const u32 maxval)
168  {
169  if (maxval==0)
170  {
171  FAIL(ILLEGAL_ARGUMENT);
172  }
173  int bitmask = _getNextPowerOf2(maxval)-1;
174  u32 ret;
175  do
176  { // loop executes less than two times on average
177  ret = Create()&bitmask;
178  } while (ret >= maxval);
179 
180  return ret;
181  }
182 
183  inline bool Random::OddsOf(u32 thisMany, u32 outOfThisMany)
184  {
185  return Create(outOfThisMany) < thisMany;
186  }
187 
188  inline s32 Random::Between(s32 min, s32 max)
189  {
190  if (max<min)
191  {
192  FAIL(ILLEGAL_ARGUMENT);
193  }
194  u32 range = (u32) (max-min+1);
195  return ((s32) Create(range)) + min;
196  }
197 } /* namespace MFM */
198 #endif
Definition: Random.h:45
Definition: FXP.h:190
void SetSeed(u32 seed)
Definition: Random.h:147
bool OneIn(u32 odds)
Definition: Random.h:96
bool OddsOf(u32 thisMany, u32 outOfThisMany)
Definition: Random.h:183
Definition: RandMT.h:63
u32 Create()
Definition: Random.h:161
Random()
Definition: Random.h:52
Random(u32 seed)
Definition: Random.h:59
bool OddsOf(FXP< P > thisMany, FXP< P > outOfThisMany)
Definition: Random.h:115
s32 Between(s32 min, s32 max)
Definition: Random.h:188
bool CreateBool()
Definition: Random.h:86