fixed typo
[anytun.git] / Sockets / RandomNumber.h
blob376db4321811758a383a1c000275f0918015095c
1 /**
2 * @author Adam McLaurin
3 * @date September 2006
4 */
5 /*
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #ifndef _SOCKET_RandomNumber_H
22 #define _SOCKET_RandomNumber_H
24 #include "sockets-config.h"
25 #include <limits>
27 #ifdef SOCKETS_NAMESPACE
28 namespace SOCKETS_NAMESPACE {
29 #endif
31 /**
32 * The following class uses an xorshift algorithm proposed in the following
33 * paper:
34 * - http://www.jstatsoft.org/v08/i14/xorshift.pdf
36 * The algorithm provides a PRNG with a period of (2^128)-1
38 * This PRNG is *not* intended for cryptographic purposes
40 class RandomNumber
43 public:
44 /**
45 * Default constructor
47 * NOTE: Internal seeds are set to defaults proposed by the paper
49 RandomNumber(bool time_shuffle = false);
51 /**
52 * Custom constructor
54 * @param x_seed X seed
56 * @param y_seed Y seed
58 * @param z_seed Z seed
60 * @param w_seed W seed
62 RandomNumber(
63 unsigned long int x_seed,
64 unsigned long int y_seed,
65 unsigned long int z_seed,
66 unsigned long int w_seed);
68 /**
69 * Destructor
71 ~RandomNumber();
73 // public methods
75 /**
76 * Reset internal state to initial seed values
78 void reset();
80 /**
81 * Cast operator to obtain current random value in the PRNG
83 * @return Current random value in the PRNG
85 operator unsigned long int() const;
87 /**
88 * Go to the next number in the PRNG sequence
90 * NOTE: This method is a slightly modified implementation of the xor128()
91 * function proposed in the paper
93 * @return Next value produced by the PRNG (after updating)
95 unsigned long int next();
97 /**
98 * Skip ahead in the PRNG sequence by a given number of iterations
100 * @param s Number of iterations to skip ahead
102 * @return Value produced by the PRNG after skipping ahead in the sequence
104 unsigned long int skip(unsigned long int s);
107 * Obtain all the initial seeds for this PRNG
109 * @param x_seed X seed (output)
111 * @param y_seed Y seed (output)
113 * @param z_seed Z seed (output)
115 * @param w_seed W seed (output)
117 void getSeed(
118 unsigned long int& x_seed,
119 unsigned long int& y_seed,
120 unsigned long int& z_seed,
121 unsigned long int& w_seed);
124 * Get the maximum possible random number from this PRNG
126 * @return Maximum possible random number from this PRNG
128 static unsigned long int max_random();
130 // public constants
133 * Default x-seed as proposed by the paper
135 static const unsigned long int X_SEED_DEFAULT;
138 * Default y-seed as proposed by the paper
140 static const unsigned long int Y_SEED_DEFAULT;
143 * Default z-seed as proposed by the paper
145 static const unsigned long int Z_SEED_DEFAULT;
148 * Default w-seed as proposed by the paper
150 static const unsigned long int W_SEED_DEFAULT;
152 private:
154 * X seed
156 unsigned long int mXSeed;
159 * Y seed
161 unsigned long int mYSeed;
164 * Z seed
166 unsigned long int mZSeed;
169 * W seed
171 unsigned long int mWSeed;
174 * X value
176 unsigned long int mX;
179 * Y value
181 unsigned long int mY;
184 * Z value
186 unsigned long int mZ;
189 * W value
191 * NOTE: This is the externally-visible next value produced by the PRNG
193 unsigned long int mW;
197 #ifdef SOCKETS_NAMESPACE
198 } // namespace SOCKETS_NAMESPACE {
199 #endif
201 #endif // _SOCKETS_RandomNumber_H