• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdecore
 

tdecore

  • tdecore
krandomsequence.cpp
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/
20
21#include <tqptrlist.h>
22
23#include "krandomsequence.h"
24#include "tdeapplication.h"
25
26const int KRandomSequence::m_nShuffleTableSize = 32;
27
29// Construction / Destruction
31
32KRandomSequence::KRandomSequence( long lngSeed1 )
33{
34 // Seed the generator
35 setSeed( lngSeed1 );
36
37
38 // Set the size of the shuffle table
39 m_ShuffleArray = new long [m_nShuffleTableSize];
40}
41
42KRandomSequence::~KRandomSequence()
43{
44 delete [] m_ShuffleArray;
45}
46
47KRandomSequence::KRandomSequence(const KRandomSequence &a)
48{
49 // Set the size of the shuffle table
50 m_ShuffleArray = new long [m_nShuffleTableSize];
51 *this = a;
52}
53
54KRandomSequence &
55KRandomSequence::operator=(const KRandomSequence &a)
56{
57 m_lngSeed1 = a.m_lngSeed1;
58 m_lngSeed2 = a.m_lngSeed2;
59 m_lngShufflePos = a.m_lngShufflePos;
60 memcpy(m_ShuffleArray, a.m_ShuffleArray, sizeof(long)*m_nShuffleTableSize);
61 return *this;
62}
63
64
66// Member Functions
68
69void KRandomSequence::setSeed( long lngSeed1 )
70{
71 // Convert the positive seed number to a negative one so that the Draw()
72 // function can intialise itself the first time it is called. We just have
73 // to make sure that the seed used != 0 as zero perpetuates itself in a
74 // sequence of random numbers.
75 if ( lngSeed1 < 0 )
76 {
77 m_lngSeed1 = -1;
78 }
79 else if (lngSeed1 == 0)
80 {
81 m_lngSeed1 = -((TDEApplication::random() & ~1)+1);
82 }
83 else
84 {
85 m_lngSeed1 = -lngSeed1;
86 }
87}
88
89static const long sMod1 = 2147483563;
90static const long sMod2 = 2147483399;
91
92void KRandomSequence::Draw()
93{
94 static const long sMM1 = sMod1 - 1;
95 static const long sA1 = 40014;
96 static const long sA2 = 40692;
97 static const long sQ1 = 53668;
98 static const long sQ2 = 52774;
99 static const long sR1 = 12211;
100 static const long sR2 = 3791;
101 static const long sDiv = 1 + sMM1 / m_nShuffleTableSize;
102
103 // Long period (>2 * 10^18) random number generator of L'Ecuyer with
104 // Bayes-Durham shuffle and added safeguards. Returns a uniform random
105 // deviate between 0.0 and 1.0 (exclusive of the endpoint values). Call
106 // with a negative number to initialize; thereafter, do not alter idum
107 // between successive deviates in a sequence. RNMX should approximate
108 // the largest floating point value that is less than 1.
109
110 int j; // Index for the shuffle table
111 long k;
112
113 // Initialise
114 if ( m_lngSeed1 <= 0 )
115 {
116 m_lngSeed2 = m_lngSeed1;
117
118 // Load the shuffle table after 8 warm-ups
119 for ( j = m_nShuffleTableSize + 7; j >= 0; j-- )
120 {
121 k = m_lngSeed1 / sQ1;
122 m_lngSeed1 = sA1 * ( m_lngSeed1 - k*sQ1) - k*sR1;
123 if ( m_lngSeed1 < 0 )
124 {
125 m_lngSeed1 += sMod1;
126 }
127
128 if ( j < m_nShuffleTableSize )
129 {
130 m_ShuffleArray[j] = m_lngSeed1;
131 }
132 }
133
134 m_lngShufflePos = m_ShuffleArray[0];
135 }
136
137 // Start here when not initializing
138
139 // Compute m_lngSeed1 = ( lngIA1*m_lngSeed1 ) % lngIM1 without overflows
140 // by Schrage's method
141 k = m_lngSeed1 / sQ1;
142 m_lngSeed1 = sA1 * ( m_lngSeed1 - k*sQ1 ) - k*sR1;
143 if ( m_lngSeed1 < 0 )
144 {
145 m_lngSeed1 += sMod1;
146 }
147
148 // Compute m_lngSeed2 = ( lngIA2*m_lngSeed2 ) % lngIM2 without overflows
149 // by Schrage's method
150 k = m_lngSeed2 / sQ2;
151 m_lngSeed2 = sA2 * ( m_lngSeed2 - k*sQ2 ) - k*sR2;
152 if ( m_lngSeed2 < 0 )
153 {
154 m_lngSeed2 += sMod2;
155 }
156
157 j = m_lngShufflePos / sDiv;
158 m_lngShufflePos = m_ShuffleArray[j] - m_lngSeed2;
159 m_ShuffleArray[j] = m_lngSeed1;
160
161 if ( m_lngShufflePos < 1 )
162 {
163 m_lngShufflePos += sMM1;
164 }
165}
166
167void
168KRandomSequence::modulate(int i)
169{
170 m_lngSeed2 -= i;
171 if ( m_lngSeed2 < 0 )
172 {
173 m_lngShufflePos += sMod2;
174 }
175 Draw();
176 m_lngSeed1 -= i;
177 if ( m_lngSeed1 < 0 )
178 {
179 m_lngSeed1 += sMod1;
180 }
181 Draw();
182}
183
184double
185KRandomSequence::getDouble()
186{
187 static const double finalAmp = 1.0 / double( sMod1 );
188 static const double epsilon = 1.2E-7;
189 static const double maxRand = 1.0 - epsilon;
190 double temp;
191 Draw();
192 // Return a value that is not one of the endpoints
193 if ( ( temp = finalAmp * m_lngShufflePos ) > maxRand )
194 {
195 // We don't want to return 1.0
196 return maxRand;
197 }
198 else
199 {
200 return temp;
201 }
202}
203
204unsigned long
205KRandomSequence::getLong(unsigned long max)
206{
207 Draw();
208
209 return max ? (((unsigned long) m_lngShufflePos) % max) : 0;
210}
211
212bool
213KRandomSequence::getBool()
214{
215 Draw();
216
217 return (((unsigned long) m_lngShufflePos) & 1);
218}
219
220class KRandomSequenceList : public TQGList
221{
222 friend class KRandomSequence;
223public:
224 KRandomSequenceList() : TQGList() { }
225 virtual void deleteItem( TQPtrCollection::Item ) {}
226};
227
228void
229KRandomSequence::randomize(TQGList *_list)
230{
231 KRandomSequenceList *list = (KRandomSequenceList *)_list;
232 KRandomSequenceList l;
233 while(list->count())
234 l.append(list->takeFirst());
235
236 list->append(l.takeFirst()); // Start with 1
237 while(l.count())
238 list->insertAt(getLong(list->count()+1), l.takeFirst());
239}
KRandomSequence
A class to create a pseudo-random sequence.
Definition: krandomsequence.h:40
KRandomSequence::operator=
KRandomSequence & operator=(const KRandomSequence &a)
Assignment.
Definition: krandomsequence.cpp:55
KRandomSequence::~KRandomSequence
virtual ~KRandomSequence()
Standard destructor.
Definition: krandomsequence.cpp:42
KRandomSequence::KRandomSequence
KRandomSequence(long lngSeed=0)
Creates a pseudo-random sequence based on the seed lngSeed.
Definition: krandomsequence.cpp:32
KRandomSequence::getLong
unsigned long getLong(unsigned long max)
Get the next number from the pseudo-random sequence.
Definition: krandomsequence.cpp:205
KRandomSequence::getDouble
double getDouble()
Get the next number from the pseudo-random sequence.
Definition: krandomsequence.cpp:185
KRandomSequence::modulate
void modulate(int i)
Modulate the random sequence.
Definition: krandomsequence.cpp:168
KRandomSequence::randomize
void randomize(TQGList *list)
Put a list in random order.
Definition: krandomsequence.cpp:229
KRandomSequence::setSeed
void setSeed(long lngSeed=0)
Restart the sequence based on lngSeed.
Definition: krandomsequence.cpp:69
KRandomSequence::getBool
bool getBool()
Get a boolean from the pseudo-random sequence.
Definition: krandomsequence.cpp:213
TDEApplication::random
static int random()
Generates a uniform random number.
Definition: tdeapplication.cpp:3393

tdecore

Skip menu "tdecore"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

tdecore

Skip menu "tdecore"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdecore by doxygen 1.9.4
This website is maintained by Timothy Pearson.