[go: up one dir, main page]

File: Random.cpp

package info (click to toggle)
spew 1.0.4-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 988 kB
  • ctags: 775
  • sloc: cpp: 5,878; sh: 5,377; makefile: 112
file content (121 lines) | stat: -rw-r--r-- 3,283 bytes parent folder | download | duplicates (2)
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
117
118
119
120
121
//////////////////////////  -*-C++-*- /////////////////////////////////////////
//
// Randon.cpp
//
// Spew
//
// Copyright (C) 2004 Hewlett-Packard Corp.
//
//
// 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 version 2 of the License.
// 
// 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 GNU
// 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.,
// 675 Mass Ave, Cambridge, MA 02139, USA.

using namespace std;

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <time.h>

#include "common.h"
#include "Random.h"

///////////////////////////////////////////////////////////////////////////////
//////////////////////////  Local constants  //////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
const u32_t LCM_A = 42871;
const u32_t LCM_C = 0;
const u32_t LCM_M = 0xffffffff;
const u32_t SCHRAGES_Q = LCM_M / LCM_A;
const u32_t SCHRAGES_R = LCM_M % LCM_A;


///////////////////////////////////////////////////////////////////////////////
//////////////////////////  Member functions  /////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

//////////////////////////  Random::Random()  /////////////////////////////////
Random::Random()
{
   mPrevRandom = (u32_t)time((time_t *)NULL);
}


//////////////////////////  Random::Random(capacity_t seed)  //////////////////
Random::Random(u32_t seed)
{
    mPrevRandom = seed;
}


//////////////////////////  Random::Random(Random &rhs)  //////////////////////
Random::Random(Random &rhs)
{
   mPrevRandom = rhs.mPrevRandom;
}


//////////////////////////  Random::getSeed()  ////////////////////////////////
u32_t Random::getSeed()
{
   return mPrevRandom;
}


//////////////////////////  Random::setSeed()  ////////////////////////////////
void Random::setSeed(u32_t newSeed)
{
   mPrevRandom = newSeed;
}


//////////////////////////  Random::getRandom32() /////////////////////////////
u32_t Random::getRandom32()
{
   u32_t schrages_k = mPrevRandom / SCHRAGES_Q;
   u32_t newRandom = (LCM_A * (mPrevRandom - schrages_k * SCHRAGES_Q)) - (SCHRAGES_R * schrages_k);

   mPrevRandom = newRandom;
   return newRandom;
}


//////////////////////////  Random::getRandom32(u32_t max)  ///////////////////
u32_t Random::getRandom32(u32_t max)
{
   return this->getRandom32() % max;
}


//////////////////////////  Random::getRandom64() /////////////////////////////
u64_t Random::getRandom64()
{
   u64_t msb = (u64_t)this->getRandom32();
   u64_t lsb = (u64_t)this->getRandom32();

   return (msb << (sizeof(u32_t) * 8)) + lsb;
}


//////////////////////////  Random::getRandom64(u64_t max)  ///////////////////
u64_t Random::getRandom64(u64_t max)
{
   return this->getRandom64() % max;
}


//////////////////////////  Random::~Random()  ////////////////////////////////
Random::~Random()
{
}