summaryrefslogtreecommitdiff
path: root/noncore/games/zsame/dropin/krandomsequence.cpp
Unidiff
Diffstat (limited to 'noncore/games/zsame/dropin/krandomsequence.cpp') (more/less context) (show whitespace changes)
-rw-r--r--noncore/games/zsame/dropin/krandomsequence.cpp240
1 files changed, 240 insertions, 0 deletions
diff --git a/noncore/games/zsame/dropin/krandomsequence.cpp b/noncore/games/zsame/dropin/krandomsequence.cpp
new file mode 100644
index 0000000..d27a1c5
--- a/dev/null
+++ b/noncore/games/zsame/dropin/krandomsequence.cpp
@@ -0,0 +1,240 @@
1/*
2 This file is part of the KDE libraries
3 Copyright (c) 1999 Sean Harmer <sh@astro.keele.ac.uk>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library 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 GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19*/
20
21#include <qlist.h>
22#include <string.h>
23
24#include "kapplication.h"
25#include "krandomsequence.h"
26
27const int KRandomSequence::m_nShuffleTableSize = 32;
28
29//////////////////////////////////////////////////////////////////////////////
30 //Construction / Destruction
31//////////////////////////////////////////////////////////////////////////////
32
33KRandomSequence::KRandomSequence( long lngSeed1 )
34{
35 // Seed the generator
36 setSeed( lngSeed1 );
37
38
39 // Set the size of the shuffle table
40 m_ShuffleArray = new long [m_nShuffleTableSize];
41}
42
43KRandomSequence::~KRandomSequence()
44{
45 delete [] m_ShuffleArray;
46}
47
48KRandomSequence::KRandomSequence(const KRandomSequence &a)
49{
50 // Set the size of the shuffle table
51 m_ShuffleArray = new long [m_nShuffleTableSize];
52 *this = a;
53}
54
55KRandomSequence &
56KRandomSequence::operator=(const KRandomSequence &a)
57{
58 m_lngSeed1 = a.m_lngSeed1;
59 m_lngSeed2 = a.m_lngSeed2;
60 m_lngShufflePos = a.m_lngShufflePos;
61 memcpy(m_ShuffleArray, a.m_ShuffleArray, sizeof(long)*m_nShuffleTableSize);
62 return *this;
63}
64
65
66//////////////////////////////////////////////////////////////////////////////
67 //Member Functions
68//////////////////////////////////////////////////////////////////////////////
69
70void KRandomSequence::setSeed( long lngSeed1 )
71{
72 // Convert the positive seed number to a negative one so that the Draw()
73 // function can intialise itself the first time it is called. We just have
74 // to make sure that the seed used != 0 as zero perpetuates itself in a
75 // sequence of random numbers.
76 if ( lngSeed1 < 0 )
77 {
78 m_lngSeed1 = -1;
79 }
80 else if (lngSeed1 == 0)
81 {
82 m_lngSeed1 = -((_random() & ~1)+1);
83 }
84 else
85 {
86 m_lngSeed1 = -lngSeed1;
87 }
88}
89
90static const long sMod1 = 2147483563;
91static const long sMod2 = 2147483399;
92
93void KRandomSequence::Draw()
94{
95 static const long sMM1 = sMod1 - 1;
96 static const long sA1 = 40014;
97 static const long sA2 = 40692;
98 static const long sQ1 = 53668;
99 static const long sQ2 = 52774;
100 static const long sR1 = 12211;
101 static const long sR2 = 3791;
102 static const long sDiv = 1 + sMM1 / m_nShuffleTableSize;
103
104 // Long period (>2 * 10^18) random number generator of L'Ecuyer with
105 // Bayes-Durham shuffle and added safeguards. Returns a uniform random
106 // deviate between 0.0 and 1.0 (exclusive of the endpoint values). Call
107 // with a negative number to initialize; thereafter, do not alter idum
108 // between successive deviates in a sequence. RNMX should approximate
109 // the largest floating point value that is less than 1.
110
111 int j; // Index for the shuffle table
112 long k;
113
114 // Initialise
115 if ( m_lngSeed1 <= 0 )
116 {
117 m_lngSeed2 = m_lngSeed1;
118
119 // Load the shuffle table after 8 warm-ups
120 for ( j = m_nShuffleTableSize + 7; j >= 0; j-- )
121 {
122 k = m_lngSeed1 / sQ1;
123 m_lngSeed1 = sA1 * ( m_lngSeed1 - k*sQ1) - k*sR1;
124 if ( m_lngSeed1 < 0 )
125 {
126 m_lngSeed1 += sMod1;
127 }
128
129 if ( j < m_nShuffleTableSize )
130 {
131 m_ShuffleArray[j] = m_lngSeed1;
132 }
133 }
134
135 m_lngShufflePos = m_ShuffleArray[0];
136 }
137
138 // Start here when not initializing
139
140 // Compute m_lngSeed1 = ( lngIA1*m_lngSeed1 ) % lngIM1 without overflows
141 // by Schrage's method
142 k = m_lngSeed1 / sQ1;
143 m_lngSeed1 = sA1 * ( m_lngSeed1 - k*sQ1 ) - k*sR1;
144 if ( m_lngSeed1 < 0 )
145 {
146 m_lngSeed1 += sMod1;
147 }
148
149 // Compute m_lngSeed2 = ( lngIA2*m_lngSeed2 ) % lngIM2 without overflows
150 // by Schrage's method
151 k = m_lngSeed2 / sQ2;
152 m_lngSeed2 = sA2 * ( m_lngSeed2 - k*sQ2 ) - k*sR2;
153 if ( m_lngSeed2 < 0 )
154 {
155 m_lngSeed2 += sMod2;
156 }
157
158 j = m_lngShufflePos / sDiv;
159 m_lngShufflePos = m_ShuffleArray[j] - m_lngSeed2;
160 m_ShuffleArray[j] = m_lngSeed1;
161
162 if ( m_lngShufflePos < 1 )
163 {
164 m_lngShufflePos += sMM1;
165 }
166}
167
168void
169KRandomSequence::modulate(int i)
170{
171 m_lngSeed2 -= i;
172 if ( m_lngSeed2 < 0 )
173 {
174 m_lngShufflePos += sMod2;
175 }
176 Draw();
177 m_lngSeed1 -= i;
178 if ( m_lngSeed1 < 0 )
179 {
180 m_lngSeed1 += sMod1;
181 }
182 Draw();
183}
184
185double
186KRandomSequence::getDouble()
187{
188 static const double finalAmp = 1.0 / double( sMod1 );
189 static const double epsilon = 1.2E-7;
190 static const double maxRand = 1.0 - epsilon;
191 double temp;
192 Draw();
193 // Return a value that is not one of the endpoints
194 if ( ( temp = finalAmp * m_lngShufflePos ) > maxRand )
195 {
196 // We don't want to return 1.0
197 return maxRand;
198 }
199 else
200 {
201 return temp;
202 }
203}
204
205unsigned long
206KRandomSequence::getLong(unsigned long max)
207{
208 Draw();
209
210 return max ? (((unsigned long) m_lngShufflePos) % max) : 0;
211}
212
213bool
214KRandomSequence::getBool()
215{
216 Draw();
217
218 return (((unsigned long) m_lngShufflePos) & 1);
219}
220
221class KRandomSequenceList : public QGList
222{
223 friend class KRandomSequence;
224public:
225 KRandomSequenceList() : QGList() { }
226 virtual void deleteItem( QCollection::Item ) {}
227};
228
229void
230KRandomSequence::randomize(QGList *_list)
231{
232 KRandomSequenceList *list = (KRandomSequenceList *)_list;
233 KRandomSequenceList l;
234 while(list->count())
235 l.append(list->takeFirst());
236
237 list->append(l.takeFirst()); // Start with 1
238 while(l.count())
239 list->insertAt(getLong(list->count()+1), l.takeFirst());
240}