Debian Codegraph @

closure-library/closure/goog/testing/pseudorandom.js

raw
1 // Copyright 2011 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 /**
16 * @fileoverview PseudoRandom provides a mechanism for generating deterministic
17 * pseudo random numbers based on a seed. Based on the Park-Miller algorithm.
18 * See http://dx.doi.org/10.1145%2F63039.63042 for details.
19 *
20 */
21
22 goog.setTestOnly('goog.testing.PseudoRandom');
23 goog.provide('goog.testing.PseudoRandom');
24
25 goog.require('goog.Disposable');
26
27
28
29 /**
30 * Class for unit testing code that uses Math.random. Generates deterministic
31 * random numbers.
32 *
33 * @param {number=} opt_seed The seed to use.
34 * @param {boolean=} opt_install Whether to install the PseudoRandom at
35 * construction time.
36 * @extends {goog.Disposable}
37 * @constructor
38 * @final
39 */
40 goog.testing.PseudoRandom = function(opt_seed, opt_install) {
41 goog.Disposable.call(this);
42
43 if (!goog.isDef(opt_seed)) {
44 opt_seed = goog.testing.PseudoRandom.seedUniquifier_++ + goog.now();
45 }
46 this.seed(opt_seed);
47
48 if (opt_install) {
49 this.install();
50 }
51 };
52 goog.inherits(goog.testing.PseudoRandom, goog.Disposable);
53
54
55 /**
56 * Helps create a unique seed.
57 * @type {number}
58 * @private
59 */
60 goog.testing.PseudoRandom.seedUniquifier_ = 0;
61
62
63 /**
64 * Constant used as part of the algorithm.
65 * @type {number}
66 */
67 goog.testing.PseudoRandom.A = 48271;
68
69
70 /**
71 * Constant used as part of the algorithm. 2^31 - 1.
72 * @type {number}
73 */
74 goog.testing.PseudoRandom.M = 2147483647;
75
76
77 /**
78 * Constant used as part of the algorithm. It is equal to M / A.
79 * @type {number}
80 */
81 goog.testing.PseudoRandom.Q = 44488;
82
83
84 /**
85 * Constant used as part of the algorithm. It is equal to M % A.
86 * @type {number}
87 */
88 goog.testing.PseudoRandom.R = 3399;
89
90
91 /**
92 * Constant used as part of the algorithm to get values from range [0, 1).
93 * @type {number}
94 */
95 goog.testing.PseudoRandom.ONE_OVER_M_MINUS_ONE =
96 1.0 / (goog.testing.PseudoRandom.M - 1);
97
98
99 /**
100 * The seed of the random sequence and also the next returned value (before
101 * normalization). Must be between 1 and M - 1 (inclusive).
102 * @type {number}
103 * @private
104 */
105 goog.testing.PseudoRandom.prototype.seed_ = 1;
106
107
108 /**
109 * Whether this PseudoRandom has been installed.
110 * @type {boolean}
111 * @private
112 */
113 goog.testing.PseudoRandom.prototype.installed_;
114
115
116 /**
117 * The original Math.random function.
118 * @type {function(): number}
119 * @private
120 */
121 goog.testing.PseudoRandom.prototype.mathRandom_;
122
123
124 /**
125 * Installs this PseudoRandom as the system number generator.
126 */
127 goog.testing.PseudoRandom.prototype.install = function() {
128 if (!this.installed_) {
129 this.mathRandom_ = Math.random;
130 Math.random = goog.bind(this.random, this);
131 this.installed_ = true;
132 }
133 };
134
135
136 /** @override */
137 goog.testing.PseudoRandom.prototype.disposeInternal = function() {
138 goog.testing.PseudoRandom.superClass_.disposeInternal.call(this);
139 this.uninstall();
140 };
141
142
143 /**
144 * Uninstalls the PseudoRandom.
145 */
146 goog.testing.PseudoRandom.prototype.uninstall = function() {
147 if (this.installed_) {
148 Math.random = this.mathRandom_;
149 this.installed_ = false;
150 }
151 };
152
153
154 /**
155 * Seed the generator.
156 *
157 * @param {number=} opt_seed The seed to use.
158 */
159 goog.testing.PseudoRandom.prototype.seed = function(opt_seed) {
160 this.seed_ = opt_seed % (goog.testing.PseudoRandom.M - 1);
161 if (this.seed_ <= 0) {
162 this.seed_ += goog.testing.PseudoRandom.M - 1;
163 }
164 };
165
166
167 /**
168 * @return {number} The next number in the sequence.
169 */
170 goog.testing.PseudoRandom.prototype.random = function() {
171 var hi = Math.floor(this.seed_ / goog.testing.PseudoRandom.Q);
172 var lo = this.seed_ % goog.testing.PseudoRandom.Q;
173 var test =
174 goog.testing.PseudoRandom.A * lo - goog.testing.PseudoRandom.R * hi;
175 if (test > 0) {
176 this.seed_ = test;
177 } else {
178 this.seed_ = test + goog.testing.PseudoRandom.M;
179 }
180 return (this.seed_ - 1) * goog.testing.PseudoRandom.ONE_OVER_M_MINUS_ONE;
181 };