vendredi 16 octobre 2020

Is this a good setup for a probability based random number generator for C?

In my opinion, I feel like this is definitely not achieving what it's supposed to do. I was needing a random number generator based off probability to determine a winner of a race. So Runner A has a 40% chance of winning, for example.

    //function definition
void createDogs(DOGS* dogList) {
    //adding info the the dogInfo struct
    strcpy(dogList[0].dogName, "Easy Rex"); //dog 1
    dogList[0].odds = 40;
    dogList[0].payoutMultiplier = 2;
    strcpy(dogList[1].dogName, "Worried Bud"); //dog 2
    dogList[1].odds = 10;
    dogList[1].payoutMultiplier = 5;
    strcpy(dogList[2].dogName, "Money Ace"); //dog 3
    dogList[2].odds = 8;
    dogList[2].payoutMultiplier = 10;
    strcpy(dogList[3].dogName, "Lucky Lady"); //dog 4
    dogList[3].odds = 15;
    dogList[3].payoutMultiplier = 15;
    strcpy(dogList[4].dogName, "Cash Dawg"); //dog 5
    dogList[4].odds = 1;
    dogList[4].payoutMultiplier = 50;
    strcpy(dogList[5].dogName, "Unlucky Brutus"); //dog 6
    dogList[5].odds = 4;
    dogList[5].payoutMultiplier = 20;
    strcpy(dogList[6].dogName, "Gamble Champ"); //dog 7
    dogList[6].odds = 8;
    dogList[6].payoutMultiplier = 10;
    strcpy(dogList[7].dogName, "Nothing Chewy"); //dog 8
    dogList[7].odds = 10;
    dogList[7].payoutMultiplier = 5;
    strcpy(dogList[8].dogName, "Easy Roxy"); //dog 9
    dogList[8].odds = 13;
    dogList[8].payoutMultiplier = 3;
}//end createDogs

So here's where I put the probability, under the "odds", then here is where I implement it. Thinking that the odds is the percentage so grabbing the random number from 0 to that "percentage" and then compare them with the others to determine the winner.

//function definition
void dogRace(DOGS* dogList, DATA* raceInfo, int counter) {
    int numberRolled[NO_OF_DOGS];
    int i, moneyWon;
    int biggestNumber, position = 0;
    srand(time(0));
    printf("\nAnd the race is on!");
    pause(5);
    for (i = 0; i < NO_OF_DOGS; i++) { //assigns a number to each dog based on its odds
        numberRolled[i] = (rand() % dogList[i].odds);
    }//end for
    biggestNumber = numberRolled[0];
    for (i = 0; i < NO_OF_DOGS; i++) { //determines which dog won (>number rolled)
        if (biggestNumber < numberRolled[i]) {
                biggestNumber = numberRolled[i];
                position = i;
            }//end if
    }//end for
}

Technically it works, but I feel as it's not really the right way to do it- let me know if there is a better way to do this because my textbooks do no give any example for this type of problem. I've also tried googling/youtubing it and had no luck finding for what I was looking for.




Aucun commentaire:

Enregistrer un commentaire