1 // Copyright 2003 "Gilles Degottex"
3 // This file is part of "CppAddons"
5 // "CppAddons" is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation; either version 2.1 of the License, or
8 // (at your option) any later version.
10 // "CppAddons" is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 const int Random::A = 48271;
25 //const int Random::M = 2147483647;
26 const int Random::M = RAND_MAX;
27 const int Random::Q = M / A;
28 const int Random::R = M % A;
30 Random Random::s_random;
32 Random::Random(long seed)
34 m_haveNextNextGaussian=false;
41 void Random::setSeed(long seed)
45 if(seed < 0) seed += M+1;
47 if(m_seed==0) m_seed = 1;
49 m_haveNextNextGaussian = false;
54 // int tmp = A * (m_seed % Q) - R * (m_seed / Q);
55 // if(tmp>=0) m_seed = tmp;
56 // else m_seed = tmp + M;
63 double Random::nextGaussian()
65 // Throw("nextGaussian", "not yet implemented");
67 // See Knuth, ACP, Section 3.4.1 Algorithm C.
68 if(m_haveNextNextGaussian)
70 m_haveNextNextGaussian = false;
71 return m_nextNextGaussian;
78 v1 = 2 * nextDouble() - 1; // between -1 and 1
79 v2 = 2 * nextDouble() - 1; // between -1 and 1
80 s = v1 * v1 + v2 * v2;
82 while (s >= 1 || s == 0);
84 double multiplier = sqrt(-2 * log(s)/s);
86 m_nextNextGaussian = v2 * multiplier;
87 m_haveNextNextGaussian = true;
89 return v1 * multiplier;