-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathParkMiller.cpp
112 lines (88 loc) · 2.12 KB
/
ParkMiller.cpp
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
//
//
// ParkMiller.cpp
//
//
#include "ParkMiller.h"
const long a = 16807;
const long m = 2147483647;
const long q = 127773;
const long r = 2836;
ParkMiller::ParkMiller(long Seed_ ) : Seed(Seed_)
{
if (Seed ==0)
Seed=1;
}
void ParkMiller::SetSeed(long Seed_)
{
Seed=Seed_;
if (Seed ==0)
Seed=1;
}
unsigned long ParkMiller::Max()
{
return m-1;
}
unsigned long ParkMiller::Min()
{
return 1;
}
long ParkMiller::GetOneRandomInteger()
{
long k;
k=Seed/q;
Seed=a*(Seed-k*q)-r*k;
if (Seed < 0)
Seed += m;
return Seed;
}
RandomParkMiller::RandomParkMiller(unsigned long Dimensionality, unsigned long Seed)
: RandomBase(Dimensionality),
InnerGenerator(Seed),
InitialSeed(Seed)
{
Reciprocal = 1/(1.0+InnerGenerator.Max());
}
RandomBase* RandomParkMiller::clone() const
{
return new RandomParkMiller(*this);
}
void RandomParkMiller::GetUniforms(MJArray& variates)
{
for (unsigned long j=0; j < GetDimensionality(); j++)
variates[j] = InnerGenerator.GetOneRandomInteger()*Reciprocal;
}
void RandomParkMiller::Skip(unsigned long numberOfPaths)
{
MJArray tmp(GetDimensionality());
for (unsigned long j=0; j < numberOfPaths; j++)
GetUniforms(tmp);
}
void RandomParkMiller::SetSeed(unsigned long Seed)
{
InitialSeed = Seed;
InnerGenerator.SetSeed(Seed);
}
void RandomParkMiller::Reset()
{
InnerGenerator.SetSeed(InitialSeed);
}
void RandomParkMiller::ResetDimensionality(unsigned long NewDimensionality)
{
RandomBase::ResetDimensionality(NewDimensionality);
InnerGenerator.SetSeed(InitialSeed);
}
/*
*
* Copyright (c) 2002
* Mark Joshi
*
* Permission to use, copy, modify, distribute and sell this
* software for any purpose is hereby
* granted without fee, provided that the above copyright notice
* appear in all copies and that both that copyright notice and
* this permission notice appear in supporting documentation.
* Mark Joshi makes no representations about the
* suitability of this software for any purpose. It is provided
* "as is" without express or implied warranty.
*/