#include "iostream" #include "CTime" #include "Iomanip" using namespace std;
Const unsigned long maxshort = 65536L;
Const unsigned long multiplier = 1194211693L;
Const unsigned long adder = 12345L; Class Randomnumber {private:unsigned long randseed; Current seed public:randomnumber (unsigned long s=0); constructor, the default value of 0 means that the seed is automatically generated by the system unsigned short Random (unsigned long n); Generates a random integer between 0:n-1 double frandom (void);
Generates a random real number between [0:1]}; Randomnumber::randomnumber (unsigned long s)//Generate seed {if (s==0) Randseed = time (0); Generate seed with system time else randseed = s; User supplied seed} unsigned short randomnumber::random (unsigned long n)//Generate random integer between 0:n-1 {randseed = multiplier * randseed
+ adder; return (unsigned short) ((randseed>>16)%n); High 16-bit randomness good, 16-bit right shift, map to (0~n-1) in the range} double randomnumber::frandom (void)//generate [0,1] random integer between {return random (Maxshort)/dou BLE (maxshort); Generates a random integer between 0~ (maxshort-1), divided by maxshort} int tosscoins (int numBercoins)//random coin toss, numbercoins for the number of throws, return the coin face up the number of times {static Randomnumber cointoss; int I, tosses = 0; The number of coins facing upwards for (i=0; i<numbercoins; i++) tosses + = Cointoss.random (2);
Random (2) indicates positive return tosses; }//Simulate random coin toss event int main () {const int ncoins = 10; Frequency const Long ntosses = 50000L; Number of analog events long I, heads[ncoins+1];
Heads[i] is the number of times I get positive int J, position;
for (j=0; j<=ncoins; j + +)//Initialize heads heads[j] = 0;
for (i=0; i<=ntosses; i++)//Repeat 50,000 times event heads[tosscoins (ncoins)]++;
for (i=0; i<=ncoins; i++)//Output frequency graph {position = Int (float (heads[i])/ntosses*72);
cout<< SETW (6) << i << "";
for (j=0; j<position-1; j + +) cout<< "";
cout<< "*" <<endl;
} return 0; }