1 : // Copyright 2011 Google Inc. 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 : #include "syzygy/core/random_number_generator.h"
16 : #include "base/logging.h"
17 :
18 : // This is a linear congruent pseudo random generator.
19 : // See: http://en.wikipedia.org/wiki/Linear_congruential_generator.
20 :
21 : namespace {
22 :
23 : const int kA = 1103515245;
24 : const int kC = 12345;
25 :
26 : } // namespace
27 :
28 : namespace core {
29 :
30 E : RandomNumberGenerator::RandomNumberGenerator(uint32 seed) : seed_(seed) {
31 E : }
32 :
33 E : uint32 RandomNumberGenerator::operator()(uint32 n) {
34 : // The generator is g(N + 1) = (g(N) * kA + kC) mod 2^32.
35 : // The use of unsigned 32 bit values yields the mod 2^32 for free.
36 E : seed_ = seed_ * kA + kC;
37 E : uint32 ret = seed_ % n;
38 E : DCHECK_GT(n, ret);
39 E : return ret;
40 E : }
41 :
42 : } // namespace core
|