dimanche 29 mars 2020

C#: How to assign random int values to variables?

I have made a dice class that includes a method which uses random.next to determine a random int value from 1-6.

Fairdice.cs:

 class FairDice
{

    //defining variable and property
    private Random rnd;
    public int Current { get; private set; }
    public FairDice()
    {
        rnd = new Random(Guid.NewGuid().GetHashCode());
        Current = 0;
    }

    public int FRoll()
    {
        Current = rnd.Next(1, 7);
        return Current;
    }

RollDice.cs that features a loop for printing.

static class RollDice
{
    public static void Roll(this int count, Action action)
    {
        for (int i = 0; i < count; i++)
        {
           action();
        }
    }
}

Program.cs includes this code, which prints out 5 random values between 1-6:

FairDice fd = new FairDice();
                    5.Roll(() => Console.WriteLine("Dice: " + fd.FRoll()));

Problem: I wish to assign the random values in each variable and store and save them in either a list or array for future use.

clarification:

lets say it prints out the numbers: 1, 2, 3, 4, 5 - i then wish to assign each value a variable: a = 1, b = 2, ... f = 5.

and/or simply store the values in an array {1, 2, 3, 4, 5}

Is there any way to do this?




Aucun commentaire:

Enregistrer un commentaire