aboutsummaryrefslogtreecommitdiff
path: root/util/random.cc
blob: 49d6195876a31aaf5e3a0c7b0f2d16bbba7c64a1 (plain)
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
// Copyright 2005-2009 The RE2 Authors.  All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Modified from Google perftools's tcmalloc_unittest.cc.

#include "util/random.h"

namespace re2 {

int32 ACMRandom::Next() {
  const int32 M = 2147483647L;   // 2^31-1
  const int32 A = 16807;
  // In effect, we are computing seed_ = (seed_ * A) % M, where M = 2^31-1
  uint32 lo = A * (int32)(seed_ & 0xFFFF);
  uint32 hi = A * (int32)((uint32)seed_ >> 16);
  lo += (hi & 0x7FFF) << 16;
  if (lo > M) {
    lo &= M;
    ++lo;
  }
  lo += hi >> 15;
  if (lo > M) {
    lo &= M;
    ++lo;
  }
  return (seed_ = (int32) lo);
}

int32 ACMRandom::Uniform(int32 n) {
  return Next() % n;
}

}  // namespace re2