Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- //@author AwesomeAvenger1 on Scratch, /u/seth1299 on Reddit
- //How many test cases you want, this determines the number of times the program resets itself to obtain numbers.
- #define NUM_NUMS 10000
- int getNum()
- {
- int num1, num2, num3, money=100, wins =0, losses=0;
- //Declare the loss% variable, this is just to get an average of the number of losses. If you take out the part that resets
- //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.
- //Regardless, even after pulling the lever over 2,000,000 times, with 11 money per spin, you will still have positive money, but
- //with 10 money per spin, you will be in the negatives. I have tested this numerous times.
- float lossPer = 0;
- while( 5 == 5 )
- {
- //Get the actual numbers
- num1 = (rand() % 3) + 1;
- num2 = (rand() % 3) + 1;
- num3 = (rand() % 3) + 1;
- //Display the numbers that spun
- printf("Num1: %d, num2: %d, num3:%d\n", num1, num2, num3);
- //Subtract money for spinning
- money -= 2;
- //If the numbers are all identical, gain money
- if(num1 == num2 && num2 == num3)
- {
- printf("You win!\n");
- money += 10;
- wins++;
- }
- //If the numbers are 1, 2, and 3 in a row, gain money
- else if( num1 < num2 && num2 < num3)
- {
- printf("You win!\n");
- money += 10;
- wins++;
- }
- //If the numbers are 3, 2, and 1 in a row, gain money
- else if( num1 > num2 && num2 > num3)
- {
- printf("You win!\n");
- money += 10;
- wins++;
- }
- //Otherwise you get nothing
- else
- {
- printf("You got nothing!\n");
- losses++;
- }
- //Calculate the loss percentage. It's the number of losses divided by the total number of spins (losses + wins)
- lossPer = ((float)losses/(float)((float)losses+(float)wins)) * 100;
- //Display all the variables
- printf("\nMoney: %d\nWins: %d\nLosses: %d\nLoss percentage: %.02f\n", money, wins, losses, lossPer );
- //Ends the calculation and declares the player bankrupt. If you only want to play the game once, take this entire function
- //and put it back in main and take out the return. Also, add a "exit(0)" call to terminate the program.
- if(money <= 0)
- {
- printf("You are bankrupt.");
- return losses+wins;
- }
- }
- }
- int main()
- {
- srand( time(0) );
- int i, sum=0, avg=0;
- //The array of all of the total number of spins before
- //the player went bankrupt.
- int nums[NUM_NUMS];
- //This is what actually runs the program.
- for( i=0; i<NUM_NUMS; i++)
- nums[i] = getNum();
- //Gets the sum of all of the numbers in the array.
- //This is a VERY BIG NUMBER.
- for( i=0; i<NUM_NUMS; i++)
- sum += nums[i];
- //Gets the average number of the array.
- //This is the amount of times, on average, it takes to spin
- //before you go bankrupt.
- avg = sum/NUM_NUMS;
- printf("\n\nAverage: %d\n\n", avg);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement