Enki  1.9
Random.h
Go to the documentation of this file.
1 /*
2  Enki - a fast 2D robot simulator
3  Copyright (C) 1999-2016 Stephane Magnenat <stephane at magnenat dot net>
4  Copyright (C) 2004-2005 Markus Waibel <markus dot waibel at epfl dot ch>
5  Copyright (c) 2004-2005 Antoine Beyeler <abeyeler at ab-ware dot com>
6  Copyright (C) 2005-2006 Laboratory of Intelligent Systems, EPFL, Lausanne
7  Copyright (C) 2006-2008 Laboratory of Robotics Systems, EPFL, Lausanne
8  See AUTHORS for details
9 
10  This program is free software; the authors of any publication
11  arising from research using this software are asked to add the
12  following reference:
13  Enki - a fast 2D robot simulator
14  http://home.gna.org/enki
15  Stephane Magnenat <stephane at magnenat dot net>,
16  Markus Waibel <markus dot waibel at epfl dot ch>
17  Laboratory of Intelligent Systems, EPFL, Lausanne.
18 
19  You can redistribute this program and/or modify
20  it under the terms of the GNU General Public License as published by
21  the Free Software Foundation; either version 2 of the License, or
22  (at your option) any later version.
23 
24  This program is distributed in the hope that it will be useful,
25  but WITHOUT ANY WARRANTY; without even the implied warranty of
26  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  GNU General Public License for more details.
28 
29  You should have received a copy of the GNU General Public License
30  along with this program; if not, write to the Free Software
31  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 */
33 
34 #ifndef __ENKI_RANDOM_H
35 #define __ENKI_RANDOM_H
36 
37 #include <cmath>
38 #include <cstdlib>
39 
44 namespace Enki
45 {
47 
48  class FastRandom
49  {
50  private:
51  unsigned long randx;
52 
53  public:
55  FastRandom(void) { randx = 0; }
57  void setSeed(unsigned long seed) { randx = seed; }
59  unsigned long get(void) { return (randx = randx*1103515245 + 12345) & 0x7fffffff; }
61  double getRange(double range) { return (static_cast<double>(get()) * range) / 2147483648.0; }
62  };
63 
65 
66  inline double uniformRand(void)
67  {
68  return double(rand())/RAND_MAX;
69  }
70 
72  struct UniformRand
73  {
74  double from;
75  double to;
76 
78  UniformRand(double from = 0.0, double to = 1.0) { this->from = from; this->to = to; }
79 
81  double operator()() const { return from + (to-from)*uniformRand(); }
82  };
83 
85 
86  inline unsigned intRand(unsigned max)
87  {
88  if (max)
89  return rand() % max;
90  else
91  return 0;
92  }
93 
95 
96  inline bool boolRand(double prob = 0.5)
97  {
98  return uniformRand() < prob;
99  }
100 
102 
103  inline double gaussianRand(double mean, double sigm)
104  {
105  // Generation using the Polar (Box-Mueller) method.
106  // Code inspired by GSL, which is a really great math lib.
107  // http://sources.redhat.com/gsl/
108  // C++ wrapper available.
109  // http://gslwrap.sourceforge.net/
110  double r, x, y;
111 
112  // Generate random number in unity circle.
113  do
114  {
115  x = uniformRand()*2 - 1;
116  y = uniformRand()*2 - 1;
117  r = x*x + y*y;
118  }
119  while (r > 1.0 || r == 0);
120 
121  // Box-Muller transform.
122  return sigm * y * sqrt (-2.0 * log(r) / r) + mean;
123  }
124 }
125 
126 #endif
double from
lower bound of uniform distribution
Definition: Random.h:74
FastRandom(void)
Construct the random generator, initialize with a seed of 0.
Definition: Random.h:55
A fast random generator.
Definition: Random.h:48
double to
upper bound of uniform distribution
Definition: Random.h:75
Enki is the namespace of the Enki simulator. All Enki functions and classes excepted the math one are...
Definition: BluetoothBase.cpp:44
unsigned long randx
value used to compute next pseudo-random value
Definition: Random.h:51
void setSeed(unsigned long seed)
Set the seed.
Definition: Random.h:57
UniformRand(double from=0.0, double to=1.0)
Constructor. Params define the range of the uniform distribution.
Definition: Random.h:78
double operator()() const
Functor operator for use with, e.g., std::generate.
Definition: Random.h:81
double gaussianRand(double mean, double sigm)
Return a random number with a gaussian distribution of a certain mean and standard deviation...
Definition: Random.h:103
bool boolRand(double prob=0.5)
Return true with a probability prob. If no argument is given, prob = 0.5.
Definition: Random.h:96
Functor to be used with <algorithm>
Definition: Random.h:72
unsigned intRand(unsigned max)
Return a number between [0;max[ in integer in a uniform distribution.
Definition: Random.h:86
double getRange(double range)
Get a random double between 0 and range, use get() internally.
Definition: Random.h:61
double uniformRand(void)
Return a number in [0;1[ in a uniform distribution.
Definition: Random.h:66