OK everyone Im stuck again. I have modified the "Mersenne Twister" so now I have a program that generates a great random number between 0 and whatever integer I specify!!! And its totally worthless!!! Sure the program is simple and quick, but what good is the algo if it has to sit right in main??? Here is the code, its the MersenneTwister.h that is important
////////////////////////////////////////////
#include
#include
#include
#include "MersenneTwister.h"
int main(void)
{
MTRand mtrand1;
int d = mtrand1.randInt( 42 );
cout << "An integer in the range [0,42]: " << d << endl;
return 0;
}
////////////////////////////////////////////
So if I display d, I get the random number. But what I need is a function that I can send my maximum to, let MT do its thing, and have the number sent back. Sure, I dont need to know what the code does If I just want a program that generates a random number. But I do need to know what it does to manpulate it in any way. So if someone can break down what //MTRand mtrand1;// and //int d = mtrand1.randInt( 42 );// exactly mean to me, or tell me how to translate them to a fucntion, that would be great. Otherwise the "MT" is worthless to a n00b like me. Oh yea, here is whats in the MersenneTwister.h Have fun, and let me know if you find an answer!!!
Thanks!
-Noods
////////////////////////////////////////////
#ifndef MERSENNETWISTER_H
#define MERSENNETWISTER_H
#include
#include
#include
class MTRand {
public:
typedef unsigned long uint32;
enum { N = 624 };
enum { SAVE = N + 1 };
protected:
enum { M = 397 };
enum { MAGIC = 0x9908b0dfU };
uint32 state[N];
uint32 *pNext;
int left;
public:
MTRand( const uint32& oneSeed );
MTRand( uint32 *const bigSeed );
MTRand();
double rand();
double rand( const double& n );
double randExc();
double randExc( const double& n );
double randDblExc();
double randDblExc( const double& n );
uint32 randInt();
uint32 randInt( const uint32& n );
double operator()() { return rand(); }
void seed( uint32 oneSeed );
void seed( uint32 *const bigSeed );
void seed();
void save( uint32* saveArray ) const;
void load( uint32 *const loadArray );
friend std::ostream& operator<<( std::ostream& os, const MTRand& mtrand );
friend std::istream& operator>>( std::istream& is, MTRand& mtrand );
protected:
void reload();
uint32 hiBit( const uint32& u ) const { return u & 0x80000000U; }
uint32 loBit( const uint32& u ) const { return u & 0x00000001U; }
uint32 loBits( const uint32& u ) const { return u & 0x7fffffffU; }
uint32 mixBits( const uint32& u, const uint32& v ) const
{ return hiBit(u) | loBits(v); }
uint32 twist( const uint32& m, const uint32& s0, const uint32& s1 ) const
{ return m ^ (mixBits(s0,s1)>>1) ^ (loBit(s1) ? MAGIC : 0U); }
static uint32 hash( time_t t, clock_t c );
};
inline MTRand::MTRand( const uint32& oneSeed )
{ seed(oneSeed); }
inline MTRand::MTRand( uint32 *const bigSeed )
{ seed(bigSeed); }
inline MTRand::MTRand()
{ seed(); }
inline double MTRand::rand()
{ return double(randInt()) * 2.3283064370807974e-10; }
inline double MTRand::rand( const double& n )
{ return rand() * n; }
inline double MTRand::randExc()
{ return double(randInt()) * 2.3283064365386963e-10; }
inline double MTRand::randExc( const double& n )
{ return randExc() * n; }
inline double MTRand::randDblExc()
{ return double( 1.0 + randInt() ) * 2.3283064359965952e-10; }
inline double MTRand::randDblExc( const double& n )
{ return randDblExc() * n; }
inline MTRand::uint32 MTRand::randInt()
{
if( left == 0 ) reload();
--left;
register uint32 s1;
s1 = *pNext++;
s1 ^= (s1 >> 11);
s1 ^= (s1 << 7) & 0x9d2c5680U;
s1 ^= (s1 << 15) & 0xefc60000U;
return ( s1 ^ (s1 >> 18) );
}
inline MTRand::uint32 MTRand::randInt( const uint32& n )
{
uint32 used = ~0;
for( uint32 m = n; m; used <<= 1, m >>= 1 ) {}
used = ~used;
uint32 i;
do
i = randInt() & used;
while( i > n );
return i;
}
inline void MTRand::seed( uint32 oneSeed )
{
register uint32 *s;
register int i;
for( i = N, s = state;
i--;
*s = oneSeed & 0xffff0000,
*s++ |= ( (oneSeed *= 69069U)++ & 0xffff0000 ) >> 16,
(oneSeed *= 69069U)++ ) {}
reload();
}
inline void MTRand::seed( uint32 *const bigSeed )
{
register uint32 *s = state, *b = bigSeed;
register int i = N;
for( ; i--; *s++ = *b++ & 0xffffffff ) {}
reload();
}
inline void MTRand::seed()
{
FILE* urandom = fopen( "/dev/urandom", "rb" );
if( urandom )
{
register uint32 *s = state;
register int i = N;
register bool success = true;
while( success && i-- )
{
success = fread( s, sizeof(uint32), 1, urandom );
*s++ &= 0xffffffff;
}
fclose(urandom);
if( success )
{
reload();
return;
}
}
seed( hash( time(NULL), clock() ) );
}
inline void MTRand::reload()
{
register uint32 *p = state;
register int i;
for( i = N - M; i--; ++p )
*p = twist( p[M], p[0], p[1] );
for( i = M; --i; ++p )
*p = twist( p[M-N], p[0], p[1] );
*p = twist( p[M-N], p[0], state[0] );
left = N, pNext = state;
}
inline MTRand::uint32 MTRand::hash( time_t t, clock_t c )
{
static uint32 differ = 0;
uint32 h1 = 0;
unsigned char *p = (unsigned char *) &t
for( size_t i = 0; i < sizeof(t); ++i )
{
h1 *= UCHAR_MAX + 2U;
h1 += p
;
}
uint32 h2 = 0;
p = (unsigned char *) &c
for( size_t j = 0; j < sizeof(c); ++j )
{
h2 *= UCHAR_MAX + 2U;
h2 += p[j];
}
return ( h1 + differ++ ) ^ h2;
}
inline void MTRand::save( uint32* saveArray ) const
{
register uint32 *sa = saveArray;
register const uint32 *s = state;
register int i = N;
for( ; i--; *sa++ = *s++ ) {}
*sa = left;
}
inline void MTRand::load( uint32 *const loadArray )
{
register uint32 *s = state;
register uint32 *la = loadArray;
register int i = N;
for( ; i--; *s++ = *la++ ) {}
left = *la;
pNext = &state[N-left];
}
inline std::ostream& operator<<( std::ostream& os, const MTRand& mtrand )
{
register const MTRand::uint32 *s = mtrand.state;
register int i = mtrand.N;
for( ; i--; os << *s++ << "\t" ) {}
return os << mtrand.left;
}
inline std::istream& operator>>( std::istream& is, MTRand& mtrand )
{
register MTRand::uint32 *s = mtrand.state;
register int i = mtrand.N;
for( ; i--; is >> *s++ ) {}
is >> mtrand.left;
mtrand.pNext = &mtrand.state[mtrand.N-mtrand.left];
return is;
}
#endif