Tuesday, March 3, 2015

Random numbers

See the randomness of C++ generated random numbers by providing a fixed seed and a random seed.  Gather the numbers in may trials and chart the results.  Charts will give you an easier way to identify patterns to evaluate the randomness.  You should modify this code to write the values to a file and import them into Excel or OO Calc to chart.

Basic steps for file implementation:
#import<fstream>

ofstream outFile;
outfile.open("output.txt");
outfile<<zero<<one<<two<<three<<four<<endl;
outfile.close();

More details:
http://intro2cs-cpp.blogspot.com/2015/03/simple-file-io.html

Code to test randomness:
//Name: <Your Name>
//Class: 2012 Fall - COSC 1415/1436.8002
//Project 0: Random Number
//Revision: 1.0
//Date: 08/15/2012
//Description: This program show the concept of random  number generation

#include <iostream>   //string input/output header file
#include<time.h>

using namespace std;  //standard class library

int main(){
  srand ( time(NULL) );         //replace the seed with a fixed number a see the effects
  int zero=0,one=0, two=0, three=0, four=0;
for(int i=0;i<10000;i++){
  Richland = rand() % 5;
  switch(Richland){
  case 0:  zero++;   break;
  case 1:  one++;    break;
  case 2:  two++;    break;
  case 3:  three++;  break;
  case 4:  four++;   break;
  }
}
cout<<"The value of zeroes: "<<zero<<endl;
cout<<"The value of ones: "<<one<<endl;
cout<<"The value of twos: "<<two<<endl;
cout<<"The value of threes: "<<three<<endl;
cout<<"The value of fours: "<<four<<endl;

  system("pause");                           //pause the program so I can read the console 
  return 0;                                  //return 0 to show end of execution 
}

No comments:

Post a Comment