Advertisement
Guest User

probability

a guest
Jul 29th, 2012
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. void getHits(int arr[], int n){
  33.     //cout << n;
  34. float randomNumber = 0.0;
  35. srand(time(NULL));
  36. for(int i = 0; i < n; i++){
  37. randomNumber = static_cast<float> (rand())% 2.0f;
  38. //cout << "random number" << randomNumber << endl;
  39.  
  40. if(randomNumber > 0 && randomNumber < 1.0f/6.0f) {
  41.     arr[0]++;
  42.     //cout << "arrr 0" << arr[0];
  43. }
  44. if(randomNumber > 1.0f/6.0f && randomNumber < 2.0f/6.0f) arr[1]++;
  45. if(randomNumber > 2.0f/6.0f && randomNumber < 3.0f/6.0f) arr[2]++;
  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. float getProbability(int num, int count){
  60.    // cout << "getprob" << num;
  61. return float(static_cast<float>(num)/count);
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement