fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. int randto(int n) {
  6. int r, maxr = (RAND_MAX / n) * n;
  7. do r = rand(); while (r >= maxr);
  8. return r % n;
  9. }
  10.  
  11. int main(void) {
  12. srand((unsigned)time(0));
  13. int len = 0;
  14. int count[1000] = {0};
  15. for (int i = 0; i < 10000000; i++) {
  16. int dealer = randto(13);
  17. int player = randto(13);
  18. // if there's a tie nothing happens
  19. if (player == dealer) {
  20. continue;
  21. }
  22. // choose player > dealer
  23. if (player > 6) {
  24. if (player > dealer) {
  25. len++; // correct, add 1 to len
  26. } else {
  27. count[len]++; // wrong, update count
  28. len = 0; // and reset len
  29. }
  30. } else {
  31. // choose player < dealer
  32. if (player < 6) {
  33. if (player < dealer) {
  34. len++; // correct, add 1 to len
  35. } else {
  36. count[len]++; // wrong, update count
  37. len = 0; // and reset len
  38. }
  39. } else {
  40. // player is exactly in the middle
  41. // choose randomly ... or (it's the same!) choose player < dealer
  42. if (player < dealer) {
  43. len++; // correct, add 1 to len
  44. } else {
  45. count[len]++; // wrong, update count
  46. len = 0; // and reset len
  47. }
  48. }
  49. }
  50. }
  51. int countlow = 0;
  52. int counthigh = 0;
  53. for (int i = 0; i < 25; i++) countlow += count[i];
  54. for (int i = 25; i < 1000; i++) counthigh += count[i];
  55. printf("losses: %d; wins: %d. Percentage %.2f\n", countlow, counthigh, (counthigh * 100.0)/countlow);
  56. return 0;
  57. }
  58.  
Success #stdin #stdout 0.33s 5388KB
stdin
Standard input is empty
stdout
losses: 2126980; wins: 2956. Percentage 0.14