Scippy

SoPlex

Sequential object-oriented simPlex

random.h
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of the class library */
4 /* SoPlex --- the Sequential object-oriented simPlex. */
5 /* */
6 /* Copyright (C) 1996-2016 Konrad-Zuse-Zentrum */
7 /* fuer Informationstechnik Berlin */
8 /* */
9 /* SoPlex is distributed under the terms of the ZIB Academic Licence. */
10 /* */
11 /* You should have received a copy of the ZIB Academic License */
12 /* along with SoPlex; see the file COPYING. If not email to soplex@zib.de. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file random.h
17  * @brief Random numbers.
18  */
19 #ifndef _RANDOM_H_
20 #define _RANDOM_H_
21 
22 #include <assert.h>
23 
24 namespace soplex
25 {
26 #define RSTEP 1103515245
27 #define RDIVIDE 65536
28 #define RADD 12345
29 #define RMULT 32768
30 
31 /**@brief Random numbers.
32  @ingroup Elementary
33 
34  Class Random provides random Real variables, i.e. a value variable that
35  gives another value each time it is accessed. It may be used just like an
36  ordinary Real by means of an overloaded cast operator Real()%.
37 */
38 class Random
39 {
40 private:
41 
42  //--------------------------------------
43  /**@name Data */
44  //@{
45  Real themin; ///< minimum random number to be returned
46  Real themax; ///< maximum random number to be returned
47  unsigned long next; ///< random seed.
48  //@}
49 
50  //--------------------------------------
51  /**@name Helpers */
52  //@{
53  /// increases rand seed and returns a pseudo random Real value in [0,1).
55  {
56  next = next * RSTEP + RADD;
57  return last_random();
58  }
59 
60  /// returns the last used random value in [0,1).
61  Real last_random() const
62  {
63  Real i = int ((next / RDIVIDE) % RMULT);
64  return ( i / RMULT );
65  }
66  //@}
67 
68 public:
69 
70  //--------------------------------------
71  /**@name Access */
72  //@{
73  /// returns lower bound of random numbers.
74  Real min() const
75  {
76  return themin;
77  }
78  /// returns upper bound of random numbers.
79  Real max() const
80  {
81  return themax;
82  }
83 
84  /// returns next random number.
85  /** When a Random variable is used where a Real value is
86  expected, a new random number within the range specified in the
87  constructor is retured.
88  */
89  operator Real()
90  {
91  return (themin + (themax - themin) * next_random());
92  }
93 
94  /// returns last random number or seed for next one.
95  Real last() const
96  {
97  return (themin + (themax - themin) * last_random());
98  }
99  //@}
100 
101  //--------------------------------------
102  /**@name Modification */
103  //@{
104  /// resets lower bound for random numbers.
105  void setMin(Real p_min)
106  {
107  themin = p_min;
108  }
109 
110  /// resets upper bound for random numbers.
111  void setMax(Real p_max)
112  {
113  themax = p_max;
114  }
115 
116  /// resets seed for next random number.
117  void setSeed(Real seed)
118  {
119  seed = (seed - themin) / (themax - themin);
120  next = static_cast<unsigned int>(seed * RMULT * RDIVIDE);
121  }
122  //@}
123 
124  //--------------------------------------
125  /**@name Debugging */
126  //@{
127  /// consistency check.
128  bool isConsistent() const
129  {
130 #ifdef ENABLE_CONSISTENCY_CHECKS
131  return themin <= themax;
132 #else
133  return true;
134 #endif
135  }
136  //@}
137 
138  //--------------------------------------
139  /**@name Constructors / destructors */
140  //@{
141  /// default constructor.
142  /** Constructs a new (pseudo) Random variable returning values between
143  \p p_min and \p p_max and using \p p_seed as seed for the random
144  variable's sequence.
145  */
146  explicit
147  Random(Real p_min = 0, Real p_max = 1, Real p_seed = 0.5)
148  : themin(p_min), themax(p_max)
149  {
150  if (p_seed < p_min || p_seed > p_max)
151  p_seed = (p_min + p_max) / 2;
152  setSeed(p_seed);
153 
154  assert(isConsistent());
155  }
156  /// destructor
158  {}
159  //@}
160 };
161 
162 } // namespace soplex
163 #endif // _RANDOM_H_
void setMax(Real p_max)
resets upper bound for random numbers.
Definition: random.h:111
Real max() const
returns upper bound of random numbers.
Definition: random.h:79
Random numbers.Class Random provides random Real variables, i.e. a value variable that gives another ...
Definition: random.h:38
Real min() const
returns lower bound of random numbers.
Definition: random.h:74
#define RSTEP
Definition: random.h:26
#define RMULT
Definition: random.h:29
unsigned long next
random seed.
Definition: random.h:47
Real themax
maximum random number to be returned
Definition: random.h:46
double Real
SOPLEX_DEBUG.
Definition: spxdefines.h:200
Real themin
minimum random number to be returned
Definition: random.h:45
void setMin(Real p_min)
resets lower bound for random numbers.
Definition: random.h:105
Real next_random()
increases rand seed and returns a pseudo random Real value in [0,1).
Definition: random.h:54
#define RDIVIDE
Definition: random.h:27
Everything should be within this namespace.
Real last_random() const
returns the last used random value in [0,1).
Definition: random.h:61
Real last() const
returns last random number or seed for next one.
Definition: random.h:95
Random(Real p_min=0, Real p_max=1, Real p_seed=0.5)
default constructor.
Definition: random.h:147
#define RADD
Definition: random.h:28
~Random()
destructor
Definition: random.h:157
void setSeed(Real seed)
resets seed for next random number.
Definition: random.h:117
bool isConsistent() const
consistency check.
Definition: random.h:128