NEURON
MLCG.cpp
Go to the documentation of this file.
1 #include <../../nrnconf.h>
2 // This may look like C code, but it is really -*- C++ -*-
3 /*
4 Copyright (C) 1989 Free Software Foundation
5 
6 This file is part of the GNU C++ Library. This library is free
7 software; you can redistribute it and/or modify it under the terms of
8 the GNU Library General Public License as published by the Free
9 Software Foundation; either version 2 of the License, or (at your
10 option) any later version. This library is distributed in the hope
11 that it will be useful, but WITHOUT ANY WARRANTY; without even the
12 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13 PURPOSE. See the GNU Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18 #ifdef __GNUG__
19 #pragma implementation
20 #endif
21 #include <MLCG.h>
22 //
23 // SEED_TABLE_SIZE must be a power of 2
24 //
25 
26 
27 #define SEED_TABLE_SIZE 32
28 
30  /* 0xbdcc47e5 */ -1110685723, /* 0x54aea45d */ 1420731485,
31  /* 0xec0df859 */ -334628775, /* 0xda84637b */ -628857989,
32  /* 0xc8c6cb4f */ -926495921, /* 0x35574b01 */ 894913281,
33  /* 0x28260b7d */ 673581949, /* 0x0d07fdbf */ 218627519,
34  /* 0x9faaeeb0 */ -1616187728, /* 0x613dd169 */ 1631441257,
35  /* 0x5ce2d818 */ 1558370328, /* 0x85b9e706 */ -2051414266,
36  /* 0xab2469db */ -1423676965, /* 0xda02b0dc */ -637357860,
37  /* 0x45c60d6e */ 1170607470, /* 0xffe49d10 */ -1794800,
38  /* 0x7224fea3 */ 1915027107, /* 0xf9684fc9 */ -110604343,
39  /* 0xfc7ee074 */ -58793868, /* 0x326ce92a */ 845998378,
40  /* 0x366d13b5 */ 913118133, /* 0x17aaa731 */ 397059889,
41  /* 0xeb83a675 */ -343693707, /* 0x7781cb32 */ 2004994866,
42  /* 0x4ec7c92d */ 1321716013, /* 0x7f187521 */ 2132309281,
43  /* 0x2cf346b4 */ 754140852, /* 0xad13310f */ -1391251185,
44  /* 0xb89cff2b */ -1197670613, /* 0x12164de1 */ 303451617,
45  /* 0xa865168d */ -1469770099, /* 0x32b56cdf */ 850750687
46 };
47 
48 MLCG::MLCG(int32_t seed1, int32_t seed2)
49 {
52  reset();
53 }
54 
55 void
57 {
60 
61  // Most people pick stupid seed numbers that do not have enough
62  // bits. In this case, if they pick a small seed number, we
63  // map that to a specific seed.
64 
65  if (seed1 < 0) {
66  seed1 = (seed1 + 2147483561);
67  seed1 = (seed1 < 0) ? -seed1 : seed1;
68  }
69 
70  if (seed2 < 0) {
71  seed2 = (seed2 + 2147483561);
72  seed2 = (seed2 < 0) ? -seed2 : seed2;
73  }
74 
75  if (seed1 > -1 && seed1 < SEED_TABLE_SIZE) {
77  } else {
78  seedOne = seed1 ^ seedTable[seed1 & (SEED_TABLE_SIZE-1)];
79  }
80 
81  if (seed2 > -1 && seed2 < SEED_TABLE_SIZE) {
83  } else {
84  seedTwo = seed2 ^ seedTable[ seed2 & (SEED_TABLE_SIZE-1) ];
85  }
86  seedOne = (seedOne % 2147483561) + 1;
87  seedTwo = (seedTwo % 2147483397) + 1;
88 }
89 
91 {
92  int32_t k = seedOne % 53668;
93 
94  seedOne = 40014 * (seedOne-k * 53668) - k * 12211;
95  if (seedOne < 0) {
96  seedOne += 2147483563;
97  }
98 
99  k = seedTwo % 52774;
100  seedTwo = 40692 * (seedTwo - k * 52774) - k * 3791;
101  if (seedTwo < 0) {
102  seedTwo += 2147483399;
103  }
104 
105  int32_t z = seedOne - seedTwo;
106  if (z < 1) {
107  z += 2147483562;
108  }
109  return( (unsigned long) z);
110 }
111 
MLCG(int32_t seed1=0, int32_t seed2=1)
Definition: MLCG.cpp:48
int32_t seed1()
Definition: MLCG.h:54
#define SEED_TABLE_SIZE
Definition: MLCG.cpp:27
static philox4x32_key_t k
Definition: nrnran123.cpp:11
static int32_t seedTable[SEED_TABLE_SIZE]
Definition: MLCG.cpp:29
int32_t initialSeedOne
Definition: MLCG.h:32
virtual void reset()
Definition: MLCG.cpp:56
int32_t seed2()
Definition: MLCG.h:67
int32_t initialSeedTwo
Definition: MLCG.h:33
virtual uint32_t asLong()
Definition: MLCG.cpp:90
int32_t seedTwo
Definition: MLCG.h:35
int32_t seedOne
Definition: MLCG.h:34
uint uint32_t
#define int32_t
Definition: nrnconf.h:1