0

So I am writing a gambling program for my c programming class. Essential its a menu for a dog track and you have to place a wager, select a dog, and the program chooses a winner and tells you if you won (how much) or lost. I use the ran() function but a specific dog that I set to win 10% of the time wins more than any other dog (including one that should win 40% of the time).

/*Programmer: John S. Bolen*/
/*Date: 3/1/19*/
/* User Will Choose to gamble, View Results, Or Quit program. 
There are 9 Dogs to Chose from with a set %chance to win and 
a set Payout Rate */
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#define SIZE 1000
#define PAUSE system("pause")
#define CLS system("cls")
#define FLUSH nothingFlush()
#define SIZE 1000

void nothingFlush() {
    char nothing;
    while (scanf("%c", &nothing) == NULL);
}

typedef struct {
    char name[100];
    float payout;
    float winOdds;
} DOG;

//Prototype Functions
void displayAllRaces(DOG dog[], int winners[], int counter, int countOne,
    int countTwo, int countThree, int countFour, int countFive, int countSix,
    int countSeven, int countEight, int countNine);
void displayMenu();
void getBet(float *bet);
char getChoice();
float getOdds();
void pickDog(DOG dog[], int *counter, int winners[], float *bet, int *countOne,
    int *countTwo, int *countThree, int *countFour, int *countFive, int *countSix,
    int *countSeven, int *countEight, int *countNine);

main() {
    char userChoice;
    DOG dog[SIZE];
    int counter = 0;
    int countOne = 0, countTwo = 0, countThree = 0, countFour = 0,
        countFive = 0, countSix = 0, countSeven = 0, countEight = 0,
        countNine = 0;
    int winners[SIZE] = { 0 };
    float bet = 0;

    do {
        userChoice = getChoice();
        switch (userChoice) {
        case 'G': // Gamble 
            getBet(&bet);
            pickDog(dog, &counter, winners, &bet, &countOne, &countTwo, &countThree,
                &countFour, &countFive, &countSix, &countSeven, &countEight, &countNine);
            break;
        case 'R': //Results
            displayAllRaces(dog, winners,counter, countOne, countTwo, countThree, countFour,
                countFive, countSix, countSeven, countEight, countNine);
            break;
        case 'L': //Leave
            printf("\n\tThank you for visiting the Dog Track\n");
            break;
        default:
            printf("\n\tERROR! -- Enter A Valid Selection...\n");
            break;

        }//End Switch
        PAUSE;
    } while (userChoice != 'L');
}

void displayAllRaces(DOG dog[], int winners[], int counter, int countOne,
    int countTwo, int countThree, int countFour, int countFive, int countSix,
    int countSeven, int countEight, int countNine) {
    int i = 0;

    CLS;
    printf("\n\tRace Results: \n");
    printf("\tDog\t\t\t\t\tNum of Wins\n");
    if (counter > 0) {
        printf("\t%s\t\t\t%i\n", dog[0].name, countOne);
        printf("\t%s\t\t\t%i\n", dog[1].name, countTwo);
        printf("\t%s\t\t%i\n", dog[2].name, countThree);
        printf("\t%s\t\t%i\n", dog[3].name, countFour);
        printf("\t%s\t\t%i\n", dog[4].name, countFive);
        printf("\t%s\t\t%i\n", dog[5].name, countSix);
        printf("\t%s\t\t%i\n", dog[6].name, countSeven);
        printf("\t%s\t\t\t%i\n", dog[7].name, countEight);
        printf("\t%s\t\t\t%i\n", dog[8].name, countNine);
    }
    else {
        printf("\n\t\n");
    }

}

void displayMenu(){
    CLS;
    printf("\n\t==================================================\n");
    printf("\n\t==                   MAIN MENU                  ==\n");
    printf("\n\t==================================================\n");
    printf("\n\t[G]amble\n");
    printf("\n\t[R]esults\n");
    printf("\n\t[L]eave\n");
    printf("\n\tEnter Your Selection:  ");
}

void getBet(float *bet) {
    float result = 0;

    printf("\n\tEnter the amount you want to gamble: ");
    printf("\n\t(Bet has to be between $1 and $1000)\n");
    scanf_s("%f", &result); FLUSH;
    if(result < 1 || result > 1000){
        printf("\n\tERROR-- Please enter a valid amount\n");
        printf("\n\tEnter the amount you want to gamble: ");
        printf("\n\t(Bet has to be between $1 and $1000)\n");
        scanf_s("%f", &result); FLUSH;
    }
    printf("\n\tThank you for placing your wager.\n\tGood luck!\n");

    *bet = result;
    PAUSE;
}

char getChoice() {
    char result;

    displayMenu();
    scanf_s("%c", &result); FLUSH;
    result = toupper(result);

    return result;
}

float getOdds() {
    float odds[9] = { 40, 10, 8, 6, 1, 4, 8, 10, 13 };

    return odds[9];
}

void pickDog(DOG dog[], int *counter, int winners[], float *bet, int *countOne,
    int *countTwo, int *countThree, int *countFour, int *countFive, int *countSix,
    int *countSeven, int *countEight, int *countNine) {
    int i = 0;
    int choice;
    float betMoney = 0;
    int winner = 0;
    CLS;

    printf("\n\tPick a dog from the list:");
    strcpy(dog[0].name, "\n\t1.Gandalf the Great");
    strcpy(dog[1].name, "\n\t2.Fenrir Greyback");
    strcpy(dog[2].name, "\n\t3.Marley the Magnificent");
    strcpy(dog[3].name, "\n\t4.Clifford the Big Red Dog");
    strcpy(dog[4].name, "\n\t5.Petey the Little Rascal");
    strcpy(dog[5].name, "\n\t6.Courage the Cowardly Dog");
    strcpy(dog[6].name, "\n\t7.Old Yeller Before the Bullet");
    strcpy(dog[7].name, "\n\t8.Pongo the Dalmatian");
    strcpy(dog[8].name, "\n\t9.Nymeria Stark\n");

    for (i = 0; i < 9; i++) {
        printf("%s", dog[i].name);
    }
    printf("\n");

    scanf_s("%i", &choice); FLUSH;
    do {
        if (choice < 1 || choice > 9) {
            printf("\n\tPlease, try again...\n");
            scanf_s("%i", &choice);
        }
    } while (choice < 1 || choice > 9);

    betMoney = *bet;

    winner = 1 + rand() % (100 - 1 + 1);
    if (winner <= 40) {
        winner = 1;
        (*countOne)++;
    }
    else if (winner <= 50) {
        winner = 2;
        (*countTwo)++;
    }
    else if (winner <= 48) {
        winner = 3;
        (*countThree)++;
    }
    else if (winner <= 64) {
        winner = 4;
        (*countFour)++;
    }
    else if (winner <= 65) {
        winner = 5;
        (*countFive)++;
    }
    else if (winner <= 69) {
        winner = 6;
        (*countSix)++;
    }
    else if (winner <= 77) {
        winner = 7;
        (*countSeven)++;
    }
    else if (winner <= 87) {
        winner = 8;
        (*countEight)++;
    }
    else if (winner <= 100) {
        winner = 9;
        (*countNine)++;
    }
    winners[*counter] = winner;
    (*counter)++;

    if (choice == winner) {
        printf("\n\tYou picked a WINNER!\n");
        if (winner == 1) {
            (*bet) = betMoney * 2;
            printf("You win $%.2f\n", *bet);
        }
        if (winner == 2) {
            (*bet) = betMoney * 5;
            printf("You win $%.2f\n", *bet);
        }
        if (winner == 3) {
            (*bet) = betMoney * 10;
            printf("You win $%.2f\n", *bet);
        }
        if (winner == 4) {
            (*bet) = betMoney * 15;
            printf("You win $%.2f\n", *bet);
        }
        if (winner == 5) {
            (*bet) = betMoney * 50;
            printf("You win $%.2f\n", *bet);
        }
        if (winner == 6) {
            (*bet) = betMoney * 20;
            printf("You win $%.2f\n", *bet);
        }
        if (winner == 7) {
            (*bet) = betMoney * 10;
            printf("You win $%.2f\n", *bet);
        }
        if (winner == 8) {
            (*bet) = betMoney * 5;
            printf("You win $%.2f\n", *bet);
        }
        if (winner == 9) {
            (*bet) = betMoney * 3;
            printf("You win $%.2f\n", *bet);
        }
    }

    else {
        printf("\n\tYou picked a LOSER!\n");
        (*bet) = (*bet) - betMoney;
    }
}

I have included the entire code for my program here (so you can test it if you wish), but only need help in fixing my rand() function. As you can see I want to randomly chose a number between 1-100. If the number is <= 40 then its dog one 40% chance to win, if the number is <= 50 (meaning its <= 50 but >40)then dog 2 wins with a 10% chance to win. I have run the program numerous times and dog 2 always seems to have the most wins after I run it x amount of times. Any help or suggestions would be greatly appreciated!

ender987
  • 23
  • 6
  • 1
    You don't appear to be seeding the random number generator. Look up the `srand` function. – Retired Ninja Mar 05 '19 at 04:44
  • Possible duplicate of [rand() not generating random numbers after modulo operation](https://stackoverflow.com/questions/8724582/rand-not-generating-random-numbers-after-modulo-operation) – Retired Ninja Mar 05 '19 at 04:46
  • This is probably a better dupe. https://stackoverflow.com/questions/1108780/why-do-i-always-get-the-same-sequence-of-random-numbers-with-rand – Retired Ninja Mar 05 '19 at 04:46
  • All the other comments here answer the question. rand() isn't of much use for random number generation. Use something like /dev/urandom in linux or whatever, if anything, MS provides for the purpose, or use openssl's functions for this... Seeding the PRNG will cause it to produce slightly more "randomness" but nothing approaching real randomness. – Bob Shaffer Mar 05 '19 at 04:59
  • So how would use srand(time(NULL)); so it is only 1-100? I appreciate all the comments, and I have read the links you have provided Ninja, but I do not understand how I would then apply it to my situation. I understand that I have to seed it (though I don't really understand what you mean by that.) I do however understand it is not true random either way, however I a simple answer would be greatly appreciated. Where do I put srand(time(NULL));? can I then keep the rand() function i have? – ender987 Mar 05 '19 at 05:10
  • 1
    Call `srand(time(NULL))` once near the beginning of your `main()` function to initialize the state of the random number generator. That should give you a different sequence or random numbers each time you run the program. – Blastfurnace Mar 05 '19 at 05:46
  • regarding: `main()` There are only two valid signatures to `main()` (regardless of what visual studio allows) They are: `int main( void )` and `int main( int argc, char *argv[] )` Notice that both valid signatures have `int` for a return type. You compiler should have told you about this. When compiling, always enable the warnings, then fix those warnings. ( for `gcc`, at a minimum use: `-Wall -Wextra -Wconversion -pedantic -std=gnu11` ) Note other compilers use different options to produce the same results – user3629249 Mar 06 '19 at 02:58
  • OT: regarding all the `if (winner == #) {` statements. Much better to use: `switch( winner )` followed by `case` statements and a `default` statement. That way, the variable `winner` is only evaluated once rather than at the beginning of each of those `if()` statements – user3629249 Mar 06 '19 at 03:01
  • regarding: *So how would use srand(time(NULL)); so it is only 1-100* the easiest way 1) at the beginning of `main()` insert the statement: `srand( (unsigned)time( NULL ) );` then at the call to `rand()` use: `value = 1+ rand() %100;` – user3629249 Mar 06 '19 at 03:03
  • regarding: ` while (scanf("%c", &nothing) == NULL);` The function: `scanf()` does not return NULL`. It can return EOF and 0 and (in the current case) 1. Only the value 1 indicates success, – user3629249 Mar 06 '19 at 03:14

0 Answers0