Почему моя функция rand () не работает должным образом - PullRequest
0 голосов
/ 05 марта 2019

Итак, я пишу азартную программу для своего класса программирования c.Essential - это меню для следа собаки, и вам нужно сделать ставку, выбрать собаку, и программа выберет победителя и сообщит вам, выиграли ли вы (сколько) или проиграли.Я использую функцию ran (), но конкретная собака, которую я настроил на победу в 10% случаев, выигрывает больше, чем любая другая собака (в том числе та, которая должна выигрывать в 40% случаев).

/*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;
    }
}

Я включил здесь весь код моей программы (чтобы вы могли проверить его, если хотите), но мне нужна только помощь в исправлении моей функции rand ().Как видите, я хочу случайным образом выбрать число от 1 до 100.Если число <= 40, то его собака с вероятностью 40% выиграет, если число <= 50 (что означает, что <= 50, но> 40), то собака 2 выигрывает с 10% шансом на победу.Я запускал программу много раз, и собака 2, кажется, всегда выигрывает больше всего, после того как я запускаю ее х раз.Любая помощь или предложения будут с благодарностью!

...