SHOW:
|
|
- or go back to the newest paste.
1 | #include <iostream> | |
2 | #include <cstdlib> | |
3 | #include <ctime> | |
4 | ||
5 | using namespace std; | |
6 | //seed for psuedo-random numbers | |
7 | //function prototype | |
8 | void getHits(int arr[], int n); | |
9 | float getProbability(int num, int count); | |
10 | //global variable (*evil*) probability array | |
11 | float probability[6]; | |
12 | int main(int argc, char* argv[]){ | |
13 | ||
14 | //declare hits array | |
15 | int hits[6] = {0,0,0,0,0,0}; | |
16 | //number of decimal places | |
17 | setprecision(4); | |
18 | //initialize number of trials | |
19 | int n; | |
20 | cout << "input number of trials"<< endl; | |
21 | cin >> n; | |
22 | //call to getHits() | |
23 | getHits(hits, n); | |
24 | //output probability | |
25 | for(int i = 0; i <6; i++){ | |
26 | cout << probability[i] << endl; | |
27 | } | |
28 | system("PAUSE"); | |
29 | return 0; | |
30 | } | |
31 | /*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*/ | |
32 | - | double randomNumber = 0.0; |
32 | + | |
33 | //cout << n; | |
34 | float randomNumber = 0.0; | |
35 | - | randomNumber = static_cast<float> (rand()% 2); |
35 | + | |
36 | for(int i = 0; i < n; i++){ | |
37 | randomNumber = static_cast<float> (rand())% 2.0f; | |
38 | - | if(randomNumber > 0 && randomNumber < 1/6) { |
38 | + | |
39 | ||
40 | if(randomNumber > 0 && randomNumber < 1.0f/6.0f) { | |
41 | arr[0]++; | |
42 | - | if(randomNumber > 1/6 && randomNumber < 2/6) arr[1]++; |
42 | + | |
43 | - | if(randomNumber > 2/6 && randomNumber < 3/6) arr[2]++; |
43 | + | |
44 | - | if(randomNumber > 3/6 && randomNumber < 4/6) arr[3]++; |
44 | + | if(randomNumber > 1.0f/6.0f && randomNumber < 2.0f/6.0f) arr[1]++; |
45 | - | if(randomNumber > 4/6 && randomNumber < 5/6) arr[4]++; |
45 | + | if(randomNumber > 2.0f/6.0f && randomNumber < 3.0f/6.0f) arr[2]++; |
46 | - | if(randomNumber > 5/6){ |
46 | + | if(randomNumber > 3.0f/6.0f && randomNumber < 4.0f/6.0f) arr[3]++; |
47 | if(randomNumber > 4.0f/6.0f && randomNumber < 5.0f/6.0f) arr[4]++; | |
48 | if(randomNumber > 5.0f/6.0f){ | |
49 | arr[5]++; | |
50 | //cout << "arr" << arr[5] << endl; | |
51 | } | |
52 | } | |
53 | for(int i = 0; i < 6; i++){ | |
54 | probability[i] = getProbability(arr[i], n); | |
55 | } | |
56 | } | |
57 | ||
58 | /*calculates probability by dividing num with count and returns the result*/ | |
59 | - | return float(num/count); |
59 | + | |
60 | // cout << "getprob" << num; | |
61 | return float(static_cast<float>(num)/count); | |
62 | } |