就是别人写的rand(同事网上找的),自己也用的着。也算记录:
class SimpleRandomNumberGenerator {
/*
| This is a simple linear congruence random number generator.
| It is *not* meant to be a good generator. (However, the
| multiplier chosen is not too bad.)
|
| The whole point of this class is that you can have several
| independend instances of SimpleRandomNumberGenerator.
*/
private:
static
const unsigned long lower_bound = 0;
static
const unsigned long upper_bound = 0xffffffff;
public:
/*
| The modulus of this generator is 2^32. The multiplier is taken
| from Knuth [line 16 in table 1 of section 3.3.4]. This shift is
| a bad guess.
*/
SimpleRandomNumberGenerator ( unsigned long _seed ) :
seed( _seed )
{}
unsigned long lower ( void ) const {
return ( lower_bound );
}
unsigned long upper ( void ) const {
return ( upper_bound );
}
unsigned long operator() ( void ) {
// return a random number in [lower(),upper()]
seed = ( seed * a + c ) & upper_bound;
return( seed );
}
unsigned long operator() ( unsigned long bound ) {
// return a random number in [0,bound)
unsigned long int_width = upper_bound / bound;
unsigned long max_valid = int_width * bound;
do {
seed = ( seed * a + c ) & upper_bound;
} while ( seed >= max_valid );
return( seed / int_width );
}
private:
mutable unsigned long seed;
static const unsigned long a = 1664525U;
static const unsigned long c = 31415927U;
}; // class SimpleRandomNumberGenerator
/*****************以下内容为2006-1-12日添加*****************/
// VAX
static const unsigned long a = 69069U;
static const unsigned long c = 0U; //or 1U
// Unix rand routine
static const unsigned long a = 1103515245U;
static const unsigned long c = 12345U;
// From the VisualC++ C run time source code
static const unsigned long a = 214013U;
static const unsigned long c = 2531011U;