Main Page   Packages   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Search  

Random Class Reference

#include <Random.hpp>

List of all members.

Public Methods

 Random ()
 Random (const ULONG Size)
 Random (const ULONG Size, const ULONG Max)
 Random (const ULONG Size, const ULONG Max, const UINT value)
 Random (Random &s)
virtual ~Random ()
void init () const
ULONG getMaximum () const
UINT getSeed () const
ULONG getSize () const
void Clear ()
void Resize (const ULONG Size)
void Backup ()
void Restore ()
void Display ()
void Randomize ()
void Randomize (const uint value)
void Verify (const ULONG spread)
void feed ()
void feedUp ()
void feedDown ()
void feedValue (Elem value)
void feed (BOOL keepBackup)
void feedUp (BOOL keepBackup)
void feedDown (BOOL keepBackup)
void feedValue (BOOL keepBackup, Elem value)
Elem getRnd ()
Elem getRnd (const ULONG Max)
ElemgetArray ()

Private Attributes

Elemarray
ElemarrayBackup
ULONG maximum
uint seed
ULONG size
BOOL filled


Constructor & Destructor Documentation

Random::Random  
 

Definition at line 9 of file Random.cpp.

00010 : maximum( RAND_MAX )
00011 {
00012   Randomize();
00013   Resize( 256 );
00014 }

Random::Random const ULONG    Size
 

Definition at line 16 of file Random.cpp.

00017 : maximum( Size )
00018 {
00019   Randomize();
00020   Resize( Size );
00021 }

Random::Random const ULONG    Size,
const ULONG    Max
 

Definition at line 23 of file Random.cpp.

00024 : maximum( Max )
00025 {
00026   Randomize();
00027   Resize( Size );
00028 }

Random::Random const ULONG    Size,
const ULONG    Max,
const UINT    value
 

Definition at line 30 of file Random.cpp.

00031 : maximum( Max )
00032 {
00033   Randomize( value );
00034   Resize( Size );
00035 }

Random::Random Random &    s
 

Random::~Random   [virtual]
 

Definition at line 52 of file Random.cpp.

00053 {
00054 //  Clear();
00055 }


Member Function Documentation

void Random::Backup  
 

Definition at line 87 of file Random.cpp.

00088 {
00089   for( register size_t i = 0; i < size; i++ ) 
00090   {
00091     arrayBackup[i] = array[i];
00092   }
00093 }

void Random::Clear  
 

Definition at line 57 of file Random.cpp.

Referenced by Resize().

00058 {
00059   if ( filled == false )  return;
00060 
00061   if ( array != NULL )
00062   {    
00063     delete[]  array;
00064   }
00065 
00066   if ( arrayBackup != NULL )
00067   {  
00068     delete[]  arrayBackup;
00069   }
00070 
00071   filled = false;
00072 }

void Random::Display  
 

Definition at line 263 of file Random.cpp.

00264 {
00265   if ( array == NULL )
00266   {
00267     return;
00268   } 
00269   else 
00270   {
00271     for( register size_t i = 0; i < size; i++ )
00272     {
00273       printf(" %d", array[i] );
00274     }
00275 
00276     printf("\n");
00277   }
00278 }

void Random::Randomize const uint    value
 

void Random::Randomize  
 

Definition at line 183 of file Random.cpp.

Referenced by Random().

00184 {
00185   // Seed the random-number generator with current time so that
00186   // the numbers will be different every time we run.
00187   seed = (UINT)time( NULL );
00188   init();
00189 }

void Random::Resize const ULONG    Size
 

Definition at line 74 of file Random.cpp.

Referenced by Random().

00075 {
00076   if ( filled == true )
00077   {
00078     Clear();
00079   }
00080 
00081   size        = Size;
00082   array       = new Elem[ size ];
00083   arrayBackup = new Elem[ size ];
00084   filled      = true;
00085 }

void Random::Restore  
 

Definition at line 95 of file Random.cpp.

00096 {
00097   for( register size_t i = 0; i < size; i++ ) 
00098   {
00099     array[i] = arrayBackup[i];
00100   }
00101 }

void Random::Verify const ULONG    spread
 

Definition at line 240 of file Random.cpp.

00241 {
00242    Elem* nb = new Elem[ spread ];
00243    register size_t i = 0;
00244 
00245    for( i = 0; i < size; i++ )
00246    {
00247      array[i] = getRnd();
00248 
00249      nb[ array[i] % spread ]++;
00250  
00251      printf( " [%6d] ", array[i] );
00252    }
00253 
00254    printf("\n\n");
00255 
00256    for( i = 0; i < spread; i++ )
00257    {
00258      double s = (double)array[i] / size * 100;
00259      printf( " %6d %.2f \t", array[i], s );
00260    }
00261 }

void Random::feed BOOL    keepBackup
 

Definition at line 123 of file Random.cpp.

00124 {
00125   init();
00126 
00127   for( register size_t i = 0; i < size; i++ ) 
00128   {
00129     array[i] = getRnd();
00130 
00131     if ( keepBackup == false )
00132     {
00133       arrayBackup[i] = array[i];
00134     }
00135   }
00136 }

void Random::feed  
 

Definition at line 103 of file Random.cpp.

00104 {
00105   feed( false );
00106 }

void Random::feedDown BOOL    keepBackup
 

Definition at line 168 of file Random.cpp.

00169 {
00170   init();
00171 
00172   for( register size_t i = 0; i < size; i++ ) 
00173   {
00174     array[i] = (Elem)( size - i );
00175 
00176     if ( keepBackup == false )
00177     {
00178       arrayBackup[i] = array[i];
00179     }
00180   }
00181 }

void Random::feedDown  
 

Definition at line 113 of file Random.cpp.

00114 {
00115   feedDown( false );
00116 }

void Random::feedUp BOOL    keepBackup
 

Definition at line 153 of file Random.cpp.

00154 {
00155   init();
00156 
00157   for( register size_t i = 0; i < size; i++ ) 
00158   {
00159     array[i] = (Elem)( i );
00160 
00161     if ( keepBackup == false )
00162     {
00163       arrayBackup[i] = array[i];
00164     }
00165   }
00166 }

void Random::feedUp  
 

Definition at line 108 of file Random.cpp.

00109 {
00110   feedUp( false );
00111 }

void Random::feedValue BOOL    keepBackup,
Elem    value
 

Definition at line 138 of file Random.cpp.

00139 {
00140   init();
00141 
00142   for( register size_t i = 0; i < size; i++ ) 
00143   {
00144     array[i] = value;
00145 
00146     if ( keepBackup == false )
00147     {
00148       arrayBackup[i] = array[i];
00149     }
00150   }
00151 }

void Random::feedValue Elem    value
 

Definition at line 118 of file Random.cpp.

00119 {
00120   feedValue( false, value );
00121 }

Elem * Random::getArray  
 

Definition at line 203 of file Random.cpp.

00204 {
00205   return array;
00206 }

ULONG Random::getMaximum   const
 

Definition at line 213 of file Random.cpp.

00214 {
00215   return maximum;
00216 }

Elem Random::getRnd const ULONG    Max
 

Definition at line 228 of file Random.cpp.

00229 {
00230   // Get a number of type Elem from a double calculation
00231   // to obtain a wide uniform spread random number
00232   // in the range from 0 to Max.
00233 
00234   double r1 = rand();
00235   double r2 = r1 / RAND_MAX * Max;
00236   
00237   return ( Elem ) floor( r2 + 0.5 );
00238 }

Elem Random::getRnd  
 

Definition at line 223 of file Random.cpp.

Referenced by Verify(), and feed().

00224 {
00225   return getRnd( maximum );
00226 }

UINT Random::getSeed   const
 

Definition at line 208 of file Random.cpp.

00209 {
00210   return seed;
00211 }

ULONG Random::getSize   const
 

Definition at line 218 of file Random.cpp.

00219 {
00220   return size;
00221 }

void Random::init   const
 

Definition at line 198 of file Random.cpp.

Referenced by Randomize(), feed(), feedDown(), feedUp(), and feedValue().

00199 {
00200   srand( seed );
00201 }


Member Data Documentation

Elem* Random::array [private]
 

Definition at line 48 of file Random.hpp.

Elem* Random::arrayBackup [private]
 

Definition at line 49 of file Random.hpp.

BOOL Random::filled [private]
 

Definition at line 53 of file Random.hpp.

ULONG Random::maximum [private]
 

Definition at line 50 of file Random.hpp.

uint Random::seed [private]
 

Definition at line 51 of file Random.hpp.

ULONG Random::size [private]
 

Definition at line 52 of file Random.hpp.


The documentation for this class was generated from the following files:
Generated on Sun Oct 14 18:49:34 2001 for Standard J2K Library by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001