lundi 4 mars 2019

Why is my rand() function not working properly

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!




Aucun commentaire:

Enregistrer un commentaire