mercredi 22 avril 2020

In C#, how do I store the amount of times random numbers equal certain sums in an array?

I'm writing a program that rolls _ amounts of _ sided dice _ times as specified by the user. This is a project with set requirements such as the class structure and the fact I have to use an array to show my results. The results are supposed to display each total I could possibly get and the amount of times I got each total after rolling for the specified amount of times.

I've written my attempt at this and fully expected it to work, but of course it did not. Rolling 1,000 times and rolling 2 dice with 6 sides, here are my results:

4) 4
6) 4
8) 4
10) 4
12) 4

The sum doesn't start at 2 it starts at 4, it rolls 20 times instead of 1,000, and all the values are the same. Any idea what could be wrong?

Here's my code:

    private int[] myTotals;
    private int possibleTotal = 2;
    private int arrayLocation = 0;
    private int myNumberOfDice;
    private string results = "";
    private static Random diceGenerator = new Random();

    public DiceFactory()
    {

    }

    public void rollDice(int numberOfRolls, int numberOfSides, int numberOfDice)
    {
        myNumberOfDice = numberOfDice;

        myTotals = new int[numberOfRolls];

        arrayLocation = possibleTotal - 2;

        for (int i = 0; i < numberOfRolls; i++) {
            myTotals[arrayLocation] = diceGenerator.Next(1, numberOfSides + 1);
            myTotals[arrayLocation]++;
        }

        while (possibleTotal <= numberOfSides * myNumberOfDice)
        {
            results += (possibleTotal) + ") " + myTotals[arrayLocation] + "\r\n";
            possibleTotal += 2;
        }
    }

    public string getResults()
    {

        return results;
    }

}



Aucun commentaire:

Enregistrer un commentaire