Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- //seed for psuedo-random numbers
- //function prototype
- void getHits(int arr[], int n);
- float getProbability(int num, int count);
- //global variable (*evil*) probability array
- float probability[6];
- int main(int argc, char* argv[]){
- //declare hits array
- int hits[6] = {0,0,0,0,0,0};
- //number of decimal places
- setprecision(4);
- //initialize number of trials
- int n;
- cout << "input number of trials"<< endl;
- cin >> n;
- //call to getHits()
- getHits(hits, n);
- //output probability
- for(int i = 0; i <6; i++){
- cout << probability[i] << endl;
- }
- system("PAUSE");
- return 0;
- }
- /*getHits() takes an array arr[] and increments a cell by 1 when its corresponding statement evaluates to true and goes through the loop n times*/
- void getHits(int arr[], int n){
- //cout << n;
- float randomNumber = 0.0;
- srand(time(NULL));
- for(int i = 0; i < n; i++){
- randomNumber = static_cast<float> (rand())% 2.0f;
- //cout << "random number" << randomNumber << endl;
- if(randomNumber > 0 && randomNumber < 1.0f/6.0f) {
- arr[0]++;
- //cout << "arrr 0" << arr[0];
- }
- if(randomNumber > 1.0f/6.0f && randomNumber < 2.0f/6.0f) arr[1]++;
- if(randomNumber > 2.0f/6.0f && randomNumber < 3.0f/6.0f) arr[2]++;
- if(randomNumber > 3.0f/6.0f && randomNumber < 4.0f/6.0f) arr[3]++;
- if(randomNumber > 4.0f/6.0f && randomNumber < 5.0f/6.0f) arr[4]++;
- if(randomNumber > 5.0f/6.0f){
- arr[5]++;
- //cout << "arr" << arr[5] << endl;
- }
- }
- for(int i = 0; i < 6; i++){
- probability[i] = getProbability(arr[i], n);
- }
- }
- /*calculates probability by dividing num with count and returns the result*/
- float getProbability(int num, int count){
- // cout << "getprob" << num;
- return float(static_cast<float>(num)/count);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement