fork download
  1. #include <iostream>
  2. #include <ctime>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6.  
  7. bool askOracle(float percentChance)
  8. {
  9. // Decides if a decision should
  10. // be done (true/false), when a
  11. // certain Percent Chance is given
  12. //
  13. // -> Don't forget to call randomize
  14. // everyone once in a while!
  15.  
  16. // generate random number between 0 and 99
  17. int randomNumber = rand() % 100;
  18.  
  19. // scale it so it's between 0 and 1
  20. float randomResult = float(randomNumber) * 0.01f;
  21.  
  22. // compare to see if it had "happened"
  23. return randomResult < percentChance;
  24. }
  25.  
  26. void dropItems()
  27. {
  28. // our event has a 90% chance (which means 0.90f)
  29. // so lets ask the "oracle" to see if it happens
  30. // or not
  31. if (askOracle(0.90f)) {
  32. // drop coins or a sword or whatever
  33. std::cout << "it happened!" << std::endl;
  34. } else {
  35. std::cout << "it didn't happen!" << std::endl;
  36. }
  37. }
  38.  
  39. int main(int argc, const char * argv[])
  40. {
  41. srand(time(0));
  42. for (int i = 0; i < 10; ++i)
  43. dropItems();
  44. return 0;
  45. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
it happened!
it happened!
it didn't happen!
it happened!
it happened!
it happened!
it happened!
it happened!
it happened!
it happened!