• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KDECore

  • sources
  • kde-4.14
  • kdelibs
  • kdecore
  • util
krandomsequence.cpp
Go to the documentation of this file.
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 "krandomsequence.h"
22 #include "krandom.h"
23 
24 static const int s_nShuffleTableSize = 32;
25 
26 class KRandomSequence::Private
27 {
28 public:
29  // Generate the random number
30  void draw();
31 
32  long lngSeed1;
33  long lngSeed2;
34  long lngShufflePos;
35  long shuffleArray[s_nShuffleTableSize];
36 };
37 
39 // Construction / Destruction
41 
42 KRandomSequence::KRandomSequence( long lngSeed1 ) : d(new Private)
43 {
44  // Seed the generator
45  setSeed( lngSeed1 );
46 }
47 
48 KRandomSequence::~KRandomSequence()
49 {
50  delete d;
51 }
52 
53 KRandomSequence::KRandomSequence(const KRandomSequence &a) : d(new Private)
54 {
55  *d = *a.d;
56 }
57 
58 KRandomSequence & KRandomSequence::operator=(const KRandomSequence &a)
59 {
60  if ( this != &a ) {
61  *d = *a.d;
62  }
63  return *this;
64 }
65 
66 
68 // Member Functions
70 
71 void KRandomSequence::setSeed( long lngSeed1 )
72 {
73  // Convert the positive seed number to a negative one so that the draw()
74  // function can intialise itself the first time it is called. We just have
75  // to make sure that the seed used != 0 as zero perpetuates itself in a
76  // sequence of random numbers.
77  if ( lngSeed1 < 0 )
78  {
79  d->lngSeed1 = -1;
80  }
81  else if (lngSeed1 == 0)
82  {
83  d->lngSeed1 = -((KRandom::random() & ~1)+1);
84  }
85  else
86  {
87  d->lngSeed1 = -lngSeed1;
88  }
89 }
90 
91 static const long sMod1 = 2147483563;
92 static const long sMod2 = 2147483399;
93 
94 void KRandomSequence::Private::draw()
95 {
96  static const long sMM1 = sMod1 - 1;
97  static const long sA1 = 40014;
98  static const long sA2 = 40692;
99  static const long sQ1 = 53668;
100  static const long sQ2 = 52774;
101  static const long sR1 = 12211;
102  static const long sR2 = 3791;
103  static const long sDiv = 1 + sMM1 / s_nShuffleTableSize;
104 
105  // Long period (>2 * 10^18) random number generator of L'Ecuyer with
106  // Bayes-Durham shuffle and added safeguards. Returns a uniform random
107  // deviate between 0.0 and 1.0 (exclusive of the endpoint values). Call
108  // with a negative number to initialize; thereafter, do not alter idum
109  // between successive deviates in a sequence. RNMX should approximate
110  // the largest floating point value that is less than 1.
111 
112  int j; // Index for the shuffle table
113  long k;
114 
115  // Initialise
116  if ( lngSeed1 <= 0 )
117  {
118  lngSeed2 = lngSeed1;
119 
120  // Load the shuffle table after 8 warm-ups
121  for ( j = s_nShuffleTableSize + 7; j >= 0; --j )
122  {
123  k = lngSeed1 / sQ1;
124  lngSeed1 = sA1 * ( lngSeed1 - k*sQ1) - k*sR1;
125  if ( lngSeed1 < 0 )
126  {
127  lngSeed1 += sMod1;
128  }
129 
130  if ( j < s_nShuffleTableSize )
131  {
132  shuffleArray[j] = lngSeed1;
133  }
134  }
135 
136  lngShufflePos = shuffleArray[0];
137  }
138 
139  // Start here when not initializing
140 
141  // Compute lngSeed1 = ( lngIA1*lngSeed1 ) % lngIM1 without overflows
142  // by Schrage's method
143  k = lngSeed1 / sQ1;
144  lngSeed1 = sA1 * ( lngSeed1 - k*sQ1 ) - k*sR1;
145  if ( lngSeed1 < 0 )
146  {
147  lngSeed1 += sMod1;
148  }
149 
150  // Compute lngSeed2 = ( lngIA2*lngSeed2 ) % lngIM2 without overflows
151  // by Schrage's method
152  k = lngSeed2 / sQ2;
153  lngSeed2 = sA2 * ( lngSeed2 - k*sQ2 ) - k*sR2;
154  if ( lngSeed2 < 0 )
155  {
156  lngSeed2 += sMod2;
157  }
158 
159  j = lngShufflePos / sDiv;
160  lngShufflePos = shuffleArray[j] - lngSeed2;
161  shuffleArray[j] = lngSeed1;
162 
163  if ( lngShufflePos < 1 )
164  {
165  lngShufflePos += sMM1;
166  }
167 }
168 
169 void
170 KRandomSequence::modulate(int i)
171 {
172  d->lngSeed2 -= i;
173  if ( d->lngSeed2 < 0 )
174  {
175  d->lngShufflePos += sMod2;
176  }
177  d->draw();
178  d->lngSeed1 -= i;
179  if ( d->lngSeed1 < 0 )
180  {
181  d->lngSeed1 += sMod1;
182  }
183  d->draw();
184 }
185 
186 double
187 KRandomSequence::getDouble()
188 {
189  static const double finalAmp = 1.0 / double( sMod1 );
190  static const double epsilon = 1.2E-7;
191  static const double maxRand = 1.0 - epsilon;
192  double temp;
193  d->draw();
194  // Return a value that is not one of the endpoints
195  if ( ( temp = finalAmp * d->lngShufflePos ) > maxRand )
196  {
197  // We don't want to return 1.0
198  return maxRand;
199  }
200  else
201  {
202  return temp;
203  }
204 }
205 
206 unsigned long
207 KRandomSequence::getLong(unsigned long max)
208 {
209  d->draw();
210 
211  return max ? (((unsigned long) d->lngShufflePos) % max) : 0;
212 }
213 
214 bool
215 KRandomSequence::getBool()
216 {
217  d->draw();
218 
219  return (((unsigned long) d->lngShufflePos) & 1);
220 }
KRandomSequence::getLong
unsigned long getLong(unsigned long max)
Get the next number from the pseudo-random sequence.
Definition: krandomsequence.cpp:207
krandom.h
double
KRandomSequence::getDouble
double getDouble()
Get the next number from the pseudo-random sequence.
Definition: krandomsequence.cpp:187
KRandomSequence::modulate
void modulate(int i)
Modulate the random sequence.
Definition: krandomsequence.cpp:170
KRandom::random
int random()
Generates a uniform random number.
Definition: krandom.cpp:32
krandomsequence.h
KRandomSequence::~KRandomSequence
virtual ~KRandomSequence()
Standard destructor.
Definition: krandomsequence.cpp:48
KRandomSequence::getBool
bool getBool()
Get a boolean from the pseudo-random sequence.
Definition: krandomsequence.cpp:215
KRandomSequence::KRandomSequence
KRandomSequence(long lngSeed=0)
Creates a pseudo-random sequence based on the seed lngSeed.
Definition: krandomsequence.cpp:42
sMod2
static const long sMod2
Definition: krandomsequence.cpp:92
sMod1
static const long sMod1
Definition: krandomsequence.cpp:91
KRandomSequence::operator=
KRandomSequence & operator=(const KRandomSequence &a)
Assignment.
Definition: krandomsequence.cpp:58
KRandomSequence
A class to create a pseudo-random sequence.
Definition: krandomsequence.h:40
KRandomSequence::setSeed
void setSeed(long lngSeed=0)
Restart the sequence based on lngSeed.
Definition: krandomsequence.cpp:71
s_nShuffleTableSize
static const int s_nShuffleTableSize
Definition: krandomsequence.cpp:24
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:22:11 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDECore

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

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal