The first is the introduction:
The code is as follows:
//random Number class//random.hpp//=====================================================#ifndef random_hpp#defineRandom_hpp#include<ctime>ConstUnsignedLongMaxshort =65536L;ConstUnsignedLongMultiplier =1194211693L;ConstUnsignedLongAdder =12345L;classrandomnumber{Private: //Current SeedUnsignedLongRandseed; Public: Randomnumber () {}; Randomnumber (unsignedLongs =0);//constructor, the default value of 0 indicates that the seed is automatically generated by the systemUnsigned ShortRandom (unsignedLongn);//generates a random integer between 0:n-1 DoubleFrandom (void);//generates a random integer between [0, 1]}; Randomnumber::randomnumber (unsignedLongS//Produce seeds{ if(s = =0) {Randseed= Time (0);//generate random seeds using system time } Else{randseed=s; }}unsigned ShortRandomnumber::random (unsignedLongN//generates a random integer between 0:n-1{randseed= Multiplier * Randseed +adder; return(unsigned Short) ((Randseed >> -) %n);}DoubleRandomnumber::frandom (void)//generates a random number between [0, 1]{ returnRandom (Maxshort)/Double(Maxshort);}#endif
//Random number//main.cpp//==============================================#include <iostream>#include<iomanip>#include"random.hpp"using namespacestd;intTosscoins (intnumbercoins) { //Random coin Toss StaticRandomnumber Cointoss (0); inttosses =0; for(inti =0; i < numbercoins; i++) {tosses+ = Cointoss.random (2); } returntosses;}intMainvoid){ //simulating random coin toss events Const intNcoins =Ten; Const LongNtosses =50000L; //Heads[i] is the number of times I get the positive LongHeads[ncoins +1]; intposition; //Initializes an array of heads for(inti =0; I < Ncoins +1; i++) {Heads[i]=0; } //Repeat 50,000 trials for(intj =0; J < Ntosses; J + +) {heads[tosscoins (ncoins)]++; } //Output Frequency Graph for(inti =0; i < ncoins; i++) {Position=int(float(Heads[i])/ntosses * the); cout<< SETW (6) << I <<" "; for(intj =0; J < Position-1; J + +) {cout<<" "; } cout<<"*"<<Endl; } System ("Pause");}
The results are as follows (frequency graph):
Random Number of stochastic algorithm