Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

KRandomSequence Class Reference

#include </home/clem/local/src/opie/noncore/games/zsame/dropin/krandomsequence.h>

List of all members.

Public Member Functions

 KRandomSequence (long lngSeed=0)
virtual ~KRandomSequence ()
 KRandomSequence (const KRandomSequence &a)
KRandomSequenceoperator= (const KRandomSequence &a)
void setSeed (long lngSeed=1)
double getDouble ()
unsigned long getLong (unsigned long max)
bool getBool ()
void randomize (QGList *list)
void modulate (int i)

Private Member Functions

void Draw ()

Private Attributes

long m_lngSeed1
long m_lngSeed2
long m_lngShufflePos
long * m_ShuffleArray
KRandomSequencePrivate * d

Static Private Attributes

static const int m_nShuffleTableSize = 32


Detailed Description

A class to create a pseudo-random sequence

Given a seed number, this class will produce a sequence of pseudo-random numbers. This would typically be used in applications like games.

In general, you should instantiate a KRandomSequence object and pass along your seed number in the constructor. From then on, simply call getDouble or getLong to obtain the next number in the sequence.

Author:
Sean Harmer <sh@astro.keele.ac.uk>

Definition at line 37 of file krandomsequence.h.


Constructor & Destructor Documentation

KRandomSequence::KRandomSequence long  lngSeed = 0  ) 
 

Creates a pseudo-random sequence based on the seed lngSeed.

A Pseudo-random sequence is different for each seed but can be reproduced by starting the sequence with the same seed.

If you need a single value which needs to be unpredictable, you need to use kapp->random() instead.

Parameters:
lngSeed Seed to initialize the sequence with. If lngSeed is 0, the sequence is initialized with a value from KApplication::random().

Definition at line 33 of file krandomsequence.cpp.

References m_nShuffleTableSize, m_ShuffleArray, and setSeed().

KRandomSequence::~KRandomSequence  )  [virtual]
 

Standard destructor

Definition at line 43 of file krandomsequence.cpp.

References m_ShuffleArray.

KRandomSequence::KRandomSequence const KRandomSequence a  ) 
 

Copy constructor

Definition at line 48 of file krandomsequence.cpp.

References m_nShuffleTableSize, and m_ShuffleArray.


Member Function Documentation

void KRandomSequence::Draw  )  [private]
 

Definition at line 93 of file krandomsequence.cpp.

References m_lngSeed1, m_lngSeed2, m_lngShufflePos, m_nShuffleTableSize, m_ShuffleArray, sMod1, and sMod2.

Referenced by getBool(), getDouble(), getLong(), and modulate().

bool KRandomSequence::getBool  ) 
 

Get a boolean from the pseudo-random sequence.

Returns:
a boolean which is either true or false

Definition at line 214 of file krandomsequence.cpp.

References Draw(), and m_lngShufflePos.

double KRandomSequence::getDouble  ) 
 

Get the next number from the pseudo-random sequence.

Returns:
a pseudo-random double value between [0,1[

Definition at line 186 of file krandomsequence.cpp.

References Draw(), m_lngShufflePos, and sMod1.

unsigned long KRandomSequence::getLong unsigned long  max  ) 
 

Get the next number from the pseudo-random sequence.

Returns:
a pseudo-random integer value between [0, max[ with 0 <= max < 1.000.000

Definition at line 206 of file krandomsequence.cpp.

References Draw(), and m_lngShufflePos.

Referenced by randomize(), and StoneField::reset().

void KRandomSequence::modulate int  i  ) 
 

Modulate the random sequence.

If S(i) is the sequence of numbers that will follow given the current state after calling modulate(i), then S(i) != S(j) for i != j and S(i) == S(j) for i == j.

This can be useful in game situation where "undo" restores the state of the random sequence. If the game modulates the random sequence with the move chosen by the player, the random sequence will be identical whenever the player "redo"-s his or hers original move, but different when the player chooses another move.

With this scenario "undo" can no longer be used to repeat a certain move over and over again until the computer reacts with a favorable response or to predict the response for a certain move based on the response to another move.

Parameters:
i the sequence identified

Definition at line 169 of file krandomsequence.cpp.

References Draw(), m_lngSeed1, m_lngSeed2, m_lngShufflePos, sMod1, and sMod2.

KRandomSequence & KRandomSequence::operator= const KRandomSequence a  ) 
 

Assignment

Definition at line 56 of file krandomsequence.cpp.

References m_lngSeed1, m_lngSeed2, m_lngShufflePos, m_nShuffleTableSize, and m_ShuffleArray.

void KRandomSequence::randomize QGList list  ) 
 

Put a list in random order.

Parameters:
list the list whose order will be modified

Definition at line 230 of file krandomsequence.cpp.

References QGList::append(), QGList::count(), getLong(), QGList::insertAt(), l, list, and QGList::takeFirst().

void KRandomSequence::setSeed long  lngSeed = 1  ) 
 

Restart the sequence based on lngSeed.

Parameters:
lngSeed Seed to initialize the sequence with. If lngSeed is 0, the sequence is initialized with a value from KApplication::random().

Definition at line 70 of file krandomsequence.cpp.

References _random(), and m_lngSeed1.

Referenced by KRandomSequence(), and StoneField::reset().


Member Data Documentation

KRandomSequencePrivate* KRandomSequence::d [private]
 

Definition at line 139 of file krandomsequence.h.

long KRandomSequence::m_lngSeed1 [private]
 

Definition at line 132 of file krandomsequence.h.

Referenced by Draw(), modulate(), operator=(), and setSeed().

long KRandomSequence::m_lngSeed2 [private]
 

Definition at line 133 of file krandomsequence.h.

Referenced by Draw(), modulate(), and operator=().

long KRandomSequence::m_lngShufflePos [private]
 

Definition at line 134 of file krandomsequence.h.

Referenced by Draw(), getBool(), getDouble(), getLong(), modulate(), and operator=().

const int KRandomSequence::m_nShuffleTableSize = 32 [static, private]
 

Definition at line 27 of file krandomsequence.cpp.

Referenced by Draw(), KRandomSequence(), and operator=().

long* KRandomSequence::m_ShuffleArray [private]
 

Definition at line 137 of file krandomsequence.h.

Referenced by Draw(), KRandomSequence(), operator=(), and ~KRandomSequence().


The documentation for this class was generated from the following files:
Generated on Sat Nov 5 17:37:57 2005 for OPIE by  doxygen 1.4.2