2 * @author Adam McLaurin
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"
27 #ifdef SOCKETS_NAMESPACE
28 namespace SOCKETS_NAMESPACE
{
32 * The following class uses an xorshift algorithm proposed in the following
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
47 * NOTE: Internal seeds are set to defaults proposed by the paper
49 RandomNumber(bool time_shuffle
= false);
54 * @param x_seed X seed
56 * @param y_seed Y seed
58 * @param z_seed Z seed
60 * @param w_seed W seed
63 unsigned long int x_seed
,
64 unsigned long int y_seed
,
65 unsigned long int z_seed
,
66 unsigned long int w_seed
);
76 * Reset internal state to initial seed values
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;
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();
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)
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();
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
;
156 unsigned long int mXSeed
;
161 unsigned long int mYSeed
;
166 unsigned long int mZSeed
;
171 unsigned long int mWSeed
;
176 unsigned long int mX
;
181 unsigned long int mY
;
186 unsigned long int mZ
;
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 {
201 #endif // _SOCKETS_RandomNumber_H