File: RandSchrange.h

package info (click to toggle)
tvc 5.0.3%2Bgit20151221.80e144e%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 3,596 kB
  • sloc: cpp: 24,088; ansic: 3,933; python: 260; sh: 23; makefile: 16
file content (41 lines) | stat: -rw-r--r-- 810 bytes parent folder | download | duplicates (3)
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
/* Copyright (C) 2010 Ion Torrent Systems, Inc. All Rights Reserved */
#ifndef RANDSCHRANGE_H
#define RANDSCHRANGE_H

/**
 * Random number generator using Park & Millers numbers and Schrange's 
 * handling of integer overflow
 */
class RandSchrange {

 public:

  const static int RandMax =  2147483646; // RANDMAX = M - 1

  RandSchrange(int seed = 1) {
    SetSeed(seed);
  }
  
  void SetSeed(int seed) {
    assert(seed != 0);
    mNext = seed;
  }

  int Rand()  {
    mNext = A * (mNext % q) - r * (mNext / q);
    if (mNext < 0) {
      mNext += M;
    }
    return mNext;
  }

 private: 
  const static int A = 16807;
  const static int M = 2147483647;   // 2^31 - 1
  const static int q = 127773;       // M / A
  const static int r = 2836;         // M % A

  int mNext;
};

#endif // RANDSCHRANGE_H