Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. #include <iostream>
  2. #include <random>
  3. #include <chrono>
  4.  
  5. void trial(int denominator, int loops) {
  6. std::random_device rd; // obtain a random number from hardware
  7. std::mt19937 rng(rd()); // seed the generator
  8. std::uniform_int_distribution<> oneToX(1, denominator); // define the range
  9. std::uniform_int_distribution<> oneTo2(1, 2); // define the range
  10.  
  11. int fishMale[10] = {0,0,0,0,0,0,0,0,0,0};
  12. int fishFemale[10] = {0,0,0,0,0,0,0,0,0,0};
  13. int totalRanges = 0; // total times the range has been counted and resetted
  14.  
  15. double averageTotalRares = 0;
  16. auto startTime = std::chrono::steady_clock::now();
  17.  
  18. for (int i=0; i<loops; ++i) {
  19. int value = oneToX(rng);
  20. //std::cout<<value<<std::endl;
  21. if (value <= 5 ) {//rare fish
  22. int fish1Rand = oneTo2(rng);
  23. if(fish1Rand==1) fishMale[0]++; if(fish1Rand==2)fishFemale[0]++;
  24. }
  25. else if (value > 5 && value <=10) {
  26. int fish2Rand = oneTo2(rng);
  27. if(fish2Rand==1) fishMale[1]++; if(fish2Rand==2)fishFemale[1]++;
  28. }
  29. else if (value > 10 && value <=15) {
  30. int fish3Rand = oneTo2(rng);
  31. if(fish3Rand==1) fishMale[2]++; if(fish3Rand==2)fishFemale[2]++;
  32. }
  33. else if (value > 15 && value <=30) {//dominant fish
  34. int fish4Rand = oneTo2(rng);
  35. if(fish4Rand==1) fishMale[3]++; if(fish4Rand==2)fishFemale[3]++;
  36. }
  37. else if (value > 30 && value <=45) {
  38. int fish5Rand = oneTo2(rng);
  39. if(fish5Rand==1) fishMale[4]++; if(fish5Rand==2)fishFemale[4]++;
  40. }
  41. else if (value > 45 && value <=60) {
  42. int fish6Rand = oneTo2(rng);
  43. if(fish6Rand==1) fishMale[5]++; if(fish6Rand==2)fishFemale[5]++;
  44. }
  45. else if (value > 60 && value <=70) {//remaining fish
  46. int fish7Rand = oneTo2(rng);
  47. if(fish7Rand==1) fishMale[6]++; if(fish7Rand==2)fishFemale[6]++;
  48. }
  49. else if (value > 70 && value <=80) {
  50. int fish8Rand = oneTo2(rng);
  51. if(fish8Rand==1) fishMale[7]++; if(fish8Rand==2)fishFemale[7]++;
  52. }
  53. else if (value > 80 && value <=90) {
  54. int fish9Rand = oneTo2(rng);
  55. if(fish9Rand==1) fishMale[8]++; if(fish9Rand==2)fishFemale[8]++;
  56. }
  57. else if (value > 90 && value <=100) {
  58. int fish10Rand = oneTo2(rng);
  59. if(fish10Rand==1) fishMale[9]++; if(fish10Rand==2)fishFemale[9]++;
  60. }
  61. else
  62. std::cout<<"Out of bounds!"<<std::endl;
  63. if ( (fishMale[0]>=1 || fishMale[0]+fishFemale[0]>=2) &&
  64. (fishMale[1]>=1 || fishMale[1]+fishFemale[1]>=2) &&
  65. (fishMale[2]>=1 || fishMale[2]+fishFemale[2]>=2) &&
  66. (fishMale[3]>=1 || fishMale[3]+fishFemale[3]>=2) &&
  67. (fishMale[4]>=1 || fishMale[4]+fishFemale[4]>=2) &&
  68. (fishMale[5]>=1 || fishMale[5]+fishFemale[5]>=2) &&
  69. (fishMale[6]>=1 || fishMale[6]+fishFemale[6]>=2) &&
  70. (fishMale[7]>=1 || fishMale[7]+fishFemale[7]>=2) &&
  71. (fishMale[8]>=1 || fishMale[8]+fishFemale[8]>=2) &&
  72. (fishMale[9]>=1 || fishMale[9]+fishFemale[9]>=2)
  73. ) {
  74. for(int j=0; j<10; ++j){
  75. fishMale[j] = 0;
  76. fishFemale[j] = 0;
  77. }
  78. totalRanges += 1;
  79. }
  80. }
  81.  
  82. auto endTime = std::chrono::steady_clock::now();
  83. std::cout <<"loops : " << loops <<std::endl;
  84. std::cout <<"totalRanges : " << totalRanges <<std::endl;
  85. averageTotalRares = (double) loops / (double) totalRanges;
  86. std::cout << "loops / totalRanges : " << averageTotalRares << std::endl << "time elapsed in seconds : " << std::chrono::duration_cast<std::chrono::seconds>(endTime - startTime).count() << "s" << std::endl;
  87. };
  88.  
  89. int main() {
  90. trial(100, 1e7);
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement