Advertisement
Guest User

Slot Machine Simulation

a guest
Jan 18th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.12 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. //@author AwesomeAvenger1 on Scratch, /u/seth1299 on Reddit
  5.  
  6. //How many test cases you want, this determines the number of times the program resets itself to obtain numbers.
  7. #define NUM_NUMS 10000
  8.  
  9. int getNum()
  10. {
  11.      int num1, num2, num3, money=100, wins =0, losses=0;
  12.  
  13.      //Declare the loss% variable, this is just to get an average of the number of losses. If you take out the part that resets
  14.      //the count and take out the exit() call, it'll stabilize at around 81.5%, which means you only have a 18.5% chance to win a spin.
  15.      //Regardless, even after pulling the lever over 2,000,000 times, with 11 money per spin, you will still have positive money, but
  16.      //with 10 money per spin, you will be in the negatives. I have tested this numerous times.
  17.     float lossPer = 0;
  18.  
  19.     while( 5 == 5 )
  20.     {
  21.  
  22.     //Get the actual numbers
  23.     num1 = (rand() % 3) + 1;
  24.     num2 = (rand() % 3) + 1;
  25.     num3 = (rand() % 3) + 1;
  26.  
  27.     //Display the numbers that spun
  28.     printf("Num1: %d, num2: %d, num3:%d\n", num1, num2, num3);
  29.  
  30.     //Subtract money for spinning
  31.     money -= 2;
  32.  
  33.     //If the numbers are all identical, gain money
  34.     if(num1 == num2 && num2 == num3)
  35.     {
  36.         printf("You win!\n");
  37.         money += 10;
  38.         wins++;
  39.     }
  40.  
  41.     //If the numbers are 1, 2, and 3 in a row, gain money
  42.     else if( num1 < num2 && num2 < num3)
  43.     {
  44.         printf("You win!\n");
  45.         money += 10;
  46.         wins++;
  47.     }
  48.  
  49.     //If the numbers are 3, 2, and 1 in a row, gain money
  50.     else if( num1 > num2 && num2 > num3)
  51.     {
  52.         printf("You win!\n");
  53.         money += 10;
  54.         wins++;
  55.     }
  56.  
  57.     //Otherwise you get nothing
  58.     else
  59.     {
  60.         printf("You got nothing!\n");
  61.         losses++;
  62.     }
  63.  
  64.     //Calculate the loss percentage. It's the number of losses divided by the total number of spins (losses + wins)
  65.     lossPer = ((float)losses/(float)((float)losses+(float)wins)) * 100;
  66.  
  67.     //Display all the variables
  68.     printf("\nMoney: %d\nWins: %d\nLosses: %d\nLoss percentage: %.02f\n", money, wins, losses, lossPer );
  69.  
  70.     //Ends the calculation and declares the player bankrupt. If you only want to play the game once, take this entire function
  71.     //and put it back in main and take out the return. Also, add a "exit(0)" call to terminate the program.
  72.     if(money <= 0)
  73.     {
  74.         printf("You are bankrupt.");
  75.         return losses+wins;
  76.     }
  77.  
  78.     }
  79. }
  80.  
  81. int main()
  82. {
  83.     srand( time(0) );
  84.  
  85.     int i, sum=0, avg=0;
  86.  
  87.     //The array of all of the total number of spins before
  88.     //the player went bankrupt.
  89.     int nums[NUM_NUMS];
  90.  
  91.     //This is what actually runs the program.
  92.     for( i=0; i<NUM_NUMS; i++)
  93.         nums[i] = getNum();
  94.  
  95.     //Gets the sum of all of the numbers in the array.
  96.     //This is a VERY BIG NUMBER.
  97.      for( i=0; i<NUM_NUMS; i++)
  98.         sum += nums[i];
  99.  
  100.     //Gets the average number of the array.
  101.     //This is the amount of times, on average, it takes to spin
  102.     //before you go bankrupt.
  103.       avg = sum/NUM_NUMS;
  104.  
  105.       printf("\n\nAverage: %d\n\n", avg);
  106.  
  107.  
  108.     return 0;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement