fork(1) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. int oneHit(int hp[]) {
  6. int target = rand() % 4; //choose a random target
  7. hp[target] -= 1; //reduces its HP by 1
  8. if( hp[target] == 0) { //if the target dies
  9. if (target == 0) { //if the target was Rag
  10. return 0; //Rag has died
  11. } else {
  12. return 1; //Rag has survived
  13. }
  14. } else { //else if nobody dies
  15. return oneHit(hp); //Bouncing Blade continues
  16. }
  17. }
  18.  
  19. int main()
  20. {
  21. srand(time(NULL));
  22. int simulations = 100000000; //Number of simulations
  23. int ragLives = 0; //Set the counter to 0
  24. for(int i=0; i<simulations; i++) {
  25. int startHp[4] = {1, 5, 8, 9}; //In order: Rag, Yogg, Ysera, Drake
  26. ragLives += oneHit(startHp); //If Rag lives, add 1 to the counter
  27. }
  28. printf("Rag survived %d of %d simulations.\n", ragLives, simulations);
  29. double ragChance = (double)ragLives/simulations;
  30. printf("%.6lf chance. Or 1 in %.1lf\n", ragChance, 1/ragChance);
  31. }
Success #stdin #stdout 4.05s 9432KB
stdin
Standard input is empty
stdout
Rag survived 3386401 of 100000000 simulations.
0.033864 chance. Or 1 in 29.5