Random Number of stochastic algorithm

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.