/*
....[@@@..[@@@..............[@.................. MUD++ is a written from
....[@..[@..[@..[@..[@..[@@@@@....[@......[@.... scratch multi-user swords and
....[@..[@..[@..[@..[@..[@..[@..[@@@@@..[@@@@@.. sorcery game written in C++.
....[@......[@..[@..[@..[@..[@....[@......[@.... This server is an ongoing
....[@......[@..[@@@@@..[@@@@@.................. development project. All
................................................ contributions are welcome.
....Copyright(C).1995.Melvin.Smith.............. Enjoy.
------------------------------------------------------------------------------
Melvin Smith (aka Fusion) msmith@hom.net
MUD++ development mailing list mudpp@van.ml.org
------------------------------------------------------------------------------
random.h
*/
#ifndef _RANDOM_H
#define _RANDOM_H
#include <sys/time.h>
#define MAX_RAND 256
#define B 31415821
// Linear Congruential Method for generating random numbers
// Notice the sequence is prebuilt and stored in an array.
// Then each call just circles through the array.
// All we care about is "pseudo-random" numbers for the MUD simulation
//
// Profile information:
// 1,000,000 random numbers on 486/80
//
// Prebuilt Non-prebuilt (calcs done each call)
// 0.70 sec CPU 0.96 sec CPU
class Random
{
private:
int random_seq[ MAX_RAND ];
int next;
public:
Random()
: next(0)
{
struct timeval t;
gettimeofday( &t, 0 );
random_seq[0] = t.tv_usec;
for( int i = 1; i < MAX_RAND; i++ )
{
// Knock off the negative bit.
random_seq[i] = ( random_seq[i-1] * B + 1 ) & 0x7fffffff;
}
}
int get();
int get( int );
int get( int, int );
};
inline int Random::get()
{
return random_seq[ (next++) % MAX_RAND ];
}
inline int Random::get( int max )
{
// Sedgewick says this is a bad way to get small range random
// numbers. Ill improve it sometime.
if( !max )
return 0;
return random_seq[ (next++) % MAX_RAND ] % max;
}
inline int Random::get( int min, int max )
{
if( max == min )
return min;
return min + (random_seq[ (next++) % MAX_RAND ] % ( max - min ) + 1);
}
extern Random randgen;
#endif