This commit was manufactured by cvs2svn to create tag 'FreeBSD-release/1.0'.
[unix-history] / gnu / lib / libg++ / libg++ / MLCG.cc
CommitLineData
78ed81a3 1// This may look like C code, but it is really -*- C++ -*-
2/*
3Copyright (C) 1989 Free Software Foundation
4
5This file is part of the GNU C++ Library. This library is free
6software; you can redistribute it and/or modify it under the terms of
7the GNU Library General Public License as published by the Free
8Software Foundation; either version 2 of the License, or (at your
9option) any later version. This library is distributed in the hope
10that it will be useful, but WITHOUT ANY WARRANTY; without even the
11implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12PURPOSE. See the GNU Library General Public License for more details.
13You should have received a copy of the GNU Library General Public
14License along with this library; if not, write to the Free Software
15Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
16*/
15637ed4
RG
17#ifdef __GNUG__
18#pragma implementation
19#endif
78ed81a3 20#include <MLCG.h>
15637ed4
RG
21//
22// SEED_TABLE_SIZE must be a power of 2
23//
24
25
26#define SEED_TABLE_SIZE 32
27
28static long seedTable[SEED_TABLE_SIZE] = {
290xbdcc47e5, 0x54aea45d, 0xec0df859, 0xda84637b,
300xc8c6cb4f, 0x35574b01, 0x28260b7d, 0x0d07fdbf,
310x9faaeeb0, 0x613dd169, 0x5ce2d818, 0x85b9e706,
320xab2469db, 0xda02b0dc, 0x45c60d6e, 0xffe49d10,
330x7224fea3, 0xf9684fc9, 0xfc7ee074, 0x326ce92a,
340x366d13b5, 0x17aaa731, 0xeb83a675, 0x7781cb32,
350x4ec7c92d, 0x7f187521, 0x2cf346b4, 0xad13310f,
360xb89cff2b, 0x12164de1, 0xa865168d, 0x32b56cdf
37};
38
39MLCG::MLCG(long seed1, long seed2)
40{
41 initialSeedOne = seed1;
42 initialSeedTwo = seed2;
43 reset();
44}
45
46void
47MLCG::reset()
48{
49 long seed1 = initialSeedOne;
50 long seed2 = initialSeedTwo;
51
52 //
53 // Most people pick stupid seed numbers that do not have enough
54 // bits. In this case, if they pick a small seed number, we
55 // map that to a specific seed.
56 //
57 if (seed1 < 0) {
58 seed1 = (seed1 + 2147483561);
59 seed1 = (seed1 < 0) ? -seed1 : seed1;
60 }
61
62 if (seed2 < 0) {
63 seed2 = (seed2 + 2147483561);
64 seed2 = (seed2 < 0) ? -seed2 : seed2;
65 }
66
67 if (seed1 > -1 && seed1 < SEED_TABLE_SIZE) {
68 seedOne = seedTable[seed1];
69 } else {
70 seedOne = seed1 ^ seedTable[seed1 & (SEED_TABLE_SIZE-1)];
71 }
72
73 if (seed2 > -1 && seed2 < SEED_TABLE_SIZE) {
74 seedTwo = seedTable[seed2];
75 } else {
76 seedTwo = seed2 ^ seedTable[ seed2 & (SEED_TABLE_SIZE-1) ];
77 }
78 seedOne = (seedOne % 2147483561) + 1;
79 seedTwo = (seedTwo % 2147483397) + 1;
80}
81
82unsigned long MLCG::asLong()
83{
84 long k = seedOne % 53668;
85
86 seedOne = 40014 * (seedOne-k * 53668) - k * 12211;
87 if (seedOne < 0) {
88 seedOne += 2147483563;
89 }
90
91 k = seedTwo % 52774;
92 seedTwo = 40692 * (seedTwo - k * 52774) - k * 3791;
93 if (seedTwo < 0) {
94 seedTwo += 2147483399;
95 }
96
97 long z = seedOne - seedTwo;
98 if (z < 1) {
99 z += 2147483562;
100 }
101 return( (unsigned long) z);
102}
103