summaryrefslogtreecommitdiff
path: root/src/Sockets/RandomNumber.cpp
blob: 77ba30dfdc3d0a5049a20964245d1c619ea4838e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**
 * @author Adam McLaurin
 * @date   September 2006
 */
/*
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GN6U General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
#include "RandomNumber.h"
#include <limits>
#include <time.h>

#ifdef SOCKETS_NAMESPACE
namespace SOCKETS_NAMESPACE {
#endif

const unsigned long int RandomNumber::X_SEED_DEFAULT = 123456789UL;
const unsigned long int RandomNumber::Y_SEED_DEFAULT = 362436069UL;
const unsigned long int RandomNumber::Z_SEED_DEFAULT = 521288629UL;
const unsigned long int RandomNumber::W_SEED_DEFAULT = 88675123UL;


RandomNumber::RandomNumber(bool time_shuffle)
:mXSeed(time_shuffle ? (unsigned long)time(NULL) ^ X_SEED_DEFAULT : X_SEED_DEFAULT)
,mYSeed(time_shuffle ? (unsigned long)time(NULL) ^ Y_SEED_DEFAULT : Y_SEED_DEFAULT)
,mZSeed(time_shuffle ? (unsigned long)time(NULL) ^ Z_SEED_DEFAULT : Z_SEED_DEFAULT)
,mWSeed(time_shuffle ? (unsigned long)time(NULL) ^ W_SEED_DEFAULT : W_SEED_DEFAULT)
{
  reset();
}

RandomNumber::RandomNumber(
  unsigned long int x_seed,
  unsigned long int y_seed,
  unsigned long int z_seed,
  unsigned long int w_seed)
:mXSeed(x_seed)
,mYSeed(y_seed)
,mZSeed(z_seed)
,mWSeed(w_seed)
{
  reset();
}

RandomNumber::~RandomNumber()
{
}

void RandomNumber::reset()
{
  mX = mXSeed;
  mY = mYSeed;
  mZ = mZSeed;
  mW = mWSeed;
}

RandomNumber::operator unsigned long int() const
{
  return(mW);
}

unsigned long int RandomNumber::next()
{
  register unsigned long int t = (mX ^ (mX<<11));

  mX = mY;

  mY = mZ;

  mZ = mW;

  return(mW = (mW ^ (mW>>19)) ^ (t ^ (t>>8)));
}

unsigned long int RandomNumber::skip(unsigned long int s)
{
  for(register unsigned long int i = 0 ; i < s ; ++i)
  {
    (void)next();
  }

  return(mW);
}

void RandomNumber::getSeed(
  unsigned long int& x_seed,
  unsigned long int& y_seed,
  unsigned long int& z_seed,
  unsigned long int& w_seed)
{
  x_seed = mXSeed;
  y_seed = mYSeed;
  z_seed = mZSeed;
  w_seed = mWSeed;
}

unsigned long int RandomNumber::max_random()
{
  return(std::numeric_limits<unsigned long int>::max());
}

#ifdef SOCKETS_NAMESPACE
} // namespace SOCKETS_NAMESPACE {
#endif