Nsound  0.9.4
RngTausworthe.cc
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 //
3 // $Id: RngTausworthe.cc 887 2015-04-26 02:49:19Z weegreenblobbie $
4 //
5 // Copyright (c) 2009 to Present Nick Hilton
6 //
7 // weegreenblobbie_yahoo_com (replace '_' with '@' and '.')
8 //
9 //-----------------------------------------------------------------------------
10 
11 //-----------------------------------------------------------------------------
12 //
13 // This program is free software; you can redistribute it and/or modify
14 // it under the terms of the GNU General Public License as published by
15 // the Free Software Foundation; either version 2 of the License, or
16 // (at your option) any later version.
17 //
18 // This program is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU Library General Public License for more details.
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with this program; if not, write to the Free Software
25 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 //
27 //-----------------------------------------------------------------------------
28 
29 #include <Nsound/RngTausworthe.h>
30 
31 #include <cmath>
32 #include <ctime>
33 
34 using namespace Nsound;
35 
36 using std::abs;
37 
38 
41  :
42  s1_(0),
43  s2_(0),
44  s3_(0)
45 {
46  setSeed(static_cast<uint32>(time(NULL)));
47 }
48 
49 uint32
51 get()
52 {
53  #define MASK 0xffffffffUL
54  #define TAUSWORTHE(s,a,b,c,d) (((s & c) << d) & MASK) ^ ((((s << a) & MASK) ^ s) >> b)
55 
56  s1_ = TAUSWORTHE(s1_, 13, 19, 4294967294UL, 12);
57  s2_ = TAUSWORTHE(s2_, 2, 25, 4294967288UL, 4);
58  s3_ = TAUSWORTHE(s3_, 3, 11, 4294967280UL, 17);
59 
60  uint32 r = s1_ ^ s2_ ^ s3_;
61 
62  return r;
63 }
64 
65 int32
68  const int32 min,
69  const int32 max)
70 {
71  return static_cast<int32>(
72  get(
73  static_cast<float64>(min),
74  static_cast<float64>(max)));
75 }
76 
77 float64
80  const float64 & min,
81  const float64 & max)
82 {
83  float64 mag = abs(max - min);
84  float64 d = mag * (static_cast<float64>(get()) / 4294967296.0) + min;
85 
86  return d;
87 }
88 
89 
93 {
94  if(this == &rhs)
95  {
96  return *this;
97  }
98 
99  s1_ = rhs.s1_;
100  s2_ = rhs.s2_;
101  s3_ = rhs.s3_;
102 
103  return * this;
104 }
105 
106 void
109 {
110  uint32 s = seed;
111 
112  // Set default seed.
113  if(s == 0)
114  {
115  s = 1;
116  }
117 
118  #define LCG(n) ((69069 * n) & 0xffffffffUL)
119  s1_ = LCG(s);
120  s2_ = LCG(s1_);
121  s3_ = LCG(s2_);
122 
123  // "warm it up"
124  get();
125  get();
126  get();
127  get();
128  get();
129  get();
130 }
131 
132 
133 
Nsound::uint32 s3_
Definition: RngTausworthe.h:76
unsigned int uint32
Definition: Nsound.h:153
RngTausworthe()
Default seed used is the number of seconds from unix epoch.
#define LCG(n)
Nsound::uint32 s1_
Definition: RngTausworthe.h:74
Nsound::uint32 s2_
Definition: RngTausworthe.h:75
double float64
Definition: Nsound.h:146
signed int int32
Definition: Nsound.h:142
#define TAUSWORTHE(s, a, b, c, d)
RngTausworthe & operator=(const RngTausworthe &rhs)
assignment operator
void setSeed(Nsound::uint32 seed)
Set the seed to use.
uint32 get()
Get a random number.