不多说,直接上代码,这是在华师大算法课上做的实验代码,C++可运行。
一. 代码
#include<iostream>
#include<time.h>
#include<cmath>
using namespace std;
class Random {
public:
Random(bool pseudo = true);
double random_real();
int random_integer(int low, int high);
int poisson(double mean);
void randomByAvg(double avg,int num);
private:
int reseed(); // Re-randomize the seed.
int seed,multiplier,add_on;
// constants for use in arithmetic operations
};
Random::Random(bool pseudo)
/*
Post: The values of seed, add_on, and multiplier are
initialized. The seed is initialized randomly only if pseudo == false.
*/
{
if (pseudo) seed = 1;
else seed = time(NULL) % INT_MAX;
multiplier = 2743;
add_on = 5923;
}
int Random::reseed()
/*
Post: The seed is replaced by a pseudorandom successor.
*/
{
seed = seed * multiplier + add_on;
r