Random coin Toss

Source: Internet
Author: User
#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; }

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.