vnl_random.h
Go to the documentation of this file.
1 // This is core/vnl/vnl_random.h
2 #ifndef vnl_random_h
3 #define vnl_random_h
4 #ifdef _MSC_VER
5 # include <vcl_msvc_warnings.h>
6 #endif
7 #include "vnl/vnl_export.h"
8 
9 //:
10 // \file
11 // \author Aaron Kotcheff (Manchester)
12 // \brief A superior random number generator
13 
14 constexpr unsigned int vnl_random_array_size = 37;
15 
16 //: A superior random number generator.
17 // Implements a new random number generator that
18 // recently appeared in the literature. It generates 32 bit
19 // numbers with a higher degree of randomness than previous
20 // generators and has a cycle of 10^354 i.e. so huge that in
21 // practice it never cycles.
22 // For the mathematics behind it see:
23 // "A New Class of Random Number Generators" G. Marsaglia and A. Zaman,
24 // Annals of Applied Probability 1991, Vol. 1, No. 3, 462.
25 class VNL_EXPORT vnl_random
26 {
27  enum {linear_congruential_multiplier = 1664525, mz_previous1 = 24};
29  unsigned long mz_seed_array[vnl_random_array_size];
30  unsigned long mz_array[vnl_random_array_size];
31  unsigned int mz_array_position;
32  int mz_borrow;
33  unsigned long linear_congruential_lrand32();
34 
37 
38  public:
39  //: Default constructor.
40  // Initializes the random number generator non-deterministically.
41  // i.e. it will generate a different series of random numbers each
42  // time the program is run.
43  vnl_random();
44 
45  //: Destructor
46  ~vnl_random();
47 
48  //: Construct with seed.
49  // Initializes the random number generator deterministically
50  // using a single ulong as the 'seed'. A linear congruential
51  // generator is used to generate the 37 ulongs needed
52  // as the real seed. The same seed will produce the
53  // same series of random numbers.
54  //
55  // 9667566 is a good seed.
56  vnl_random(unsigned long seed);
57 
58  //: Construct with seed.
59  // Initializes the random number generator deterministically
60  // using 37 ulongs as the 'seed'. The same seed will
61  // produce the same series of random numbers.
62  vnl_random(unsigned long seed[vnl_random_array_size]);
63 
64  //: Copy constructor.
65  // Initializes/sets the random number generator to exactly
66  // the same state as the argument, i.e. both will generate exactly
67  // the same series of random numbers from then on.
68  vnl_random(const vnl_random&);
69 
70  //: Copy operator.
71  // Initializes/sets the random number generator to exactly
72  // the same state as the argument, i.e. both will generate exactly
73  // the same series of random numbers from then on.
74  vnl_random& operator=(const vnl_random&);
75 
76  //: Starts a new non-deterministic sequence from an already declared generator.
77  void reseed();
78 
79  //: Starts a new deterministic sequence from an already declared generator using the provided seed.
80  void reseed(unsigned long);
81 
82  //: Starts a new deterministic sequence from an already declared generator using the provided seed.
83  void reseed(const unsigned long[vnl_random_array_size]);
84 
85  //: This restarts the sequence of random numbers.
86  // Restarts so that it repeats
87  // from the point at which you declared the generator, last
88  // initialized it, or last called a 'reseed'.
89  void restart();
90 
91  //: Generates a random unsigned 32-bit number.
92  unsigned long lrand32();
93 
94  //: Generates a random unsigned long in [a,b]
95  int lrand32(int a, int b);
96 
97  //: Generates a random unsigned long in [0,b]
98  int lrand32(int b) {return lrand32(0, b);}
99 
100  //: Generates a random unsigned long in [a,b]
101  int lrand32(int a, int b, int&);
102 
103  //: Generates a random double in the range a <= x <= b with 32 bit randomness.
104  // drand32(1,0) is random down to about the 10th decimal place.
105  double drand32(double a, double b);
106 
107  //: Generates a random unsigned integer in [0,n)
108  // This function allows the random number generator to be used as
109  // a functor, e.g. with std::random_shuffle()
110  unsigned long operator()(unsigned n) { return lrand32(0, n-1); }
111 
112  //: Generates a random double in the range 0 <= x <= b with 32 bit randomness.
113  // drand32(1.0) is random down to about the 10th decimal place.
114  double drand32(double b) {return drand32(0.0, b);}
115 
116  //: Generates a random double in the range 0 <= x <= 1 with 32 bit randomness.
117  // drand32() is random down to about the 10th decimal place.
118  double drand32() {return drand32(0.0, 1.0);}
119 
120  //: Generates a random double in the range a <= x <= b with 64 bit randomness.
121  // Completely random down to the accuracy of an IEEE double.
122  double drand64(double a, double b);
123 
124  //: Generates a random double in the range 0 <= x <= b with 64 bit randomness.
125  // Completely random down to the accuracy of an IEEE double.
126  double drand64(double b) {return drand64(0.0, b);}
127 
128  //: Generates a random double in the range 0 <= x <= 1 with 64 bit randomness.
129  // Completely random down to the accuracy of an IEEE double.
130  double drand64() {return drand64(0.0, 1.0);}
131 
132  //: Random value from a unit normal distribution about zero.
133  // Uses a drand32() as its underlying generator.
134  // Because the function uses a probability transform, the randomness (and
135  // quantisation) is non-linearly dependent on the value. The further the
136  // sample is from zero, the lower the number of bits on which it is random.
137  double normal();
138 
139  //: Random value from a unit normal distribution about zero.
140  // Uses a drand64() as its underlying generator.
141  // Because the function uses a probability transform, the randomness (and
142  // quantisation) is non-linearly dependent on the value. The further the
143  // sample is from zero, the lower the number of bits on which it is random.
144  double normal64();
145 };
146 
147 #endif // vnl_random_h
int mz_borrow
Definition: vnl_random.h:32
double drand32(double b)
Generates a random double in the range 0 <= x <= b with 32 bit randomness.
Definition: vnl_random.h:114
A superior random number generator.
Definition: vnl_random.h:25
double mz_previous_normal
Definition: vnl_random.h:35
unsigned int mz_array_position
Definition: vnl_random.h:31
unsigned long operator()(unsigned n)
Generates a random unsigned integer in [0,n).
Definition: vnl_random.h:110
unsigned long linear_congruential_previous
Definition: vnl_random.h:28
int mz_previous_normal_flag
Definition: vnl_random.h:36
double drand32()
Generates a random double in the range 0 <= x <= 1 with 32 bit randomness.
Definition: vnl_random.h:118
int lrand32(int b)
Generates a random unsigned long in [0,b].
Definition: vnl_random.h:98
double drand64()
Generates a random double in the range 0 <= x <= 1 with 64 bit randomness.
Definition: vnl_random.h:130
constexpr unsigned int vnl_random_array_size
Definition: vnl_random.h:14
double drand64(double b)
Generates a random double in the range 0 <= x <= b with 64 bit randomness.
Definition: vnl_random.h:126