Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 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. if (value <= 5 ) {//rare fish
  21. int fish1Rand = oneTo2(rng);
  22. if(fish1Rand==1) fishMale[0]++; if(fish1Rand==2)fishFemale[0]++;
  23. }
  24. if (value > 5 && value <=10) {
  25. int fish2Rand = oneTo2(rng);
  26. if(fish2Rand==1) fishMale[1]++; if(fish2Rand==2)fishFemale[1]++;
  27. }
  28. if (value > 10 && value <=15) {
  29. int fish3Rand = oneTo2(rng);
  30. if(fish3Rand==1) fishMale[2]++; if(fish3Rand==2)fishFemale[2]++;
  31. }
  32. if (value > 15 && value <=30) {//dominant fish
  33. int fish4Rand = oneTo2(rng);
  34. if(fish4Rand==1) fishMale[3]++; if(fish4Rand==2)fishFemale[3]++;
  35. }
  36. if (value > 30 && value <=45) {
  37. int fish5Rand = oneTo2(rng);
  38. if(fish5Rand==1) fishMale[4]++; if(fish5Rand==2)fishFemale[4]++;
  39. }
  40. if (value > 45 && value <=60) {
  41. int fish6Rand = oneTo2(rng);
  42. if(fish6Rand==1) fishMale[5]++; if(fish6Rand==2)fishFemale[5]++;
  43. }
  44. if (value > 60 && value <=70) {//remaining fish
  45. int fish7Rand = oneTo2(rng);
  46. if(fish7Rand==1) fishMale[6]++; if(fish7Rand==2)fishFemale[6]++;
  47. }
  48. if (value > 70 && value <=80) {
  49. int fish8Rand = oneTo2(rng);
  50. if(fish8Rand==1) fishMale[7]++; if(fish8Rand==2)fishFemale[7]++;
  51. }
  52. if (value > 80 && value <=90) {
  53. int fish9Rand = oneTo2(rng);
  54. if(fish9Rand==1) fishMale[8]++; if(fish9Rand==2)fishFemale[8]++;
  55. }
  56. if (value > 90 && value <=100) {
  57. int fish10Rand = oneTo2(rng);
  58. if(fish10Rand==1) fishMale[9]++; if(fish10Rand==2)fishFemale[9]++;
  59. }
  60. if ( (fishMale[0]>=1 && fishMale[1]>=1 && fishMale[2]>=1 && fishMale[3]>=1 && fishMale[4]>=1 &&
  61. fishMale[5]>=1 && fishMale[6]>=1 && fishMale[7]>=1 && fishMale[8]>=1 && fishMale[9]>=1) ||
  62. (fishMale[0]+fishFemale[0]>=2 && fishMale[1]+fishFemale[1]>=2 && fishMale[2]+fishFemale[2]>=2 &&
  63. fishMale[3]+fishFemale[3]>=2 && fishMale[4]+fishFemale[4]>=2 && fishMale[5]+fishFemale[5]>=2 &&
  64. fishMale[6]+fishFemale[6]>=2 && fishMale[7]+fishFemale[7]>=2 && fishMale[8]+fishFemale[8]>=2 &&
  65. fishMale[9]+fishFemale[9]>=2)
  66. ) {
  67. for(int j=0; j<10; ++j){
  68. fishMale[j] = 0;
  69. fishFemale[j] = 0;
  70. }
  71. totalRanges += 1;
  72. }
  73. }
  74.  
  75. auto endTime = std::chrono::steady_clock::now();
  76. std::cout <<"loops : " << loops <<std::endl;
  77. std::cout <<"totalRanges : " << totalRanges <<std::endl;
  78. averageTotalRares = (double) loops / (double) totalRanges;
  79. std::cout << "loops / totalRanges : " << averageTotalRares << std::endl << "time elapsed in seconds : " << std::chrono::duration_cast<std::chrono::seconds>(endTime - startTime).count() << "s" << std::endl;
  80. };
  81.  
  82. int main() {
  83. trial(100, 1e6);
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement