-1

I begun my career as a trainee developer in c# this week. Got an exercise in which I should write a c#-console code for a dice-program. The specifications of the programme are:

the user is asked to type in how often the dice may be rolled/thrown the result has to be displayed in %, for each eye/number of the dice like: the dice rolled 1 about 3% of x-Times. 2 was rolled 7% of x-Times by the dice and so on.. until 6 . because I'm a newb at all in c#, this is how far I came:

   var dict = new Dictionary<int, int>();

        foreach (var value in array)
        {
            if (dict.ContainsKey(value))
                dict[value]++;
            else
                dict[value] = 1;
        }

        foreach (var pair in dict)
            Console.WriteLine("Die Zahl {0} ist {1} mal vorhanden.", pair.Key, pair.Value);
        Console.ReadKey();

and

        Random diceRandom = new Random();
        int rollDice = diceRandom.Next(1, 7);
        Console.WriteLine(diceRandom);


        //Console.WriteLine("Es wurde eine:" + rollDice + "gewürfelt" );

        for (int i = 1; i <= 10; i++)
        {
            List<int> liste = new List<int>();
            var ausgabe = liste.Select(zahlen => rollDice % 2 == 0).ToList();
            rollDice = diceRandom.Next(1, 7);
            Console.WriteLine("Es wurde eine  "  + rollDice + "  gewürfelt.");
            Thread.Sleep(50);

My problems, I've not figured out how to ask the user for the input how often the dice has to be rolled ? The second thing with output in % shouldn't be a huge drama, cause I get it somehow and first code is a beginning of a solution for that spec. It's my first exercise.

Thanks, L.J.

L.Jov
  • 13
  • 3

3 Answers3

3

I took the curtsy of rebuilding your code, for the sake of simplicity.

I've used the Console.ReadLine to get from the user the number of rolled times.

Console.WriteLine("How many times should the dice be rolled ?");
int numberOfRolledTimes = int.Parse(Console.ReadLine());
var diceRandom = new Random();

var numberAndTimesArray = new int[6];
for (int i = 0; i < numberOfRolledTimes; i++)
{
    var rolledResult = diceRandom.Next(1, 7);
    numberAndTimesArray[rolledResult - 1]++;
}

for (int i = 0; i < 6; i++)
{
    double precentage = 100.0*numberAndTimesArray[i] / numberOfRolledTimes*1.0;
    Console.WriteLine("Dice result of {0} rolled {1} times. ({2}%)", i + 1, numberAndTimesArray[i], precentage);
}

Output:

enter image description here

Orel Eraki
  • 11,940
  • 3
  • 28
  • 36
  • Your answer is great. It appears so. Grateful that you took the time and given it a practical explanation! – L.Jov Sep 08 '17 at 12:18
  • +1 -- You could even use `for (int i = 0; i < numberAndTimesArray.Length; i++)` instead of `for (int i = 0; i < 6; i++)`. -- or have a `const int sidesOfDie = 6;` and use that to avoid "magic" numbers. (`diceRandom.Next(1, 1 + sidesOfDie)`) -- and `numberOfRolledTimes*1.0` (presumably to "make it double") shouldn't be necessary, because `100.0*numberAndTimesArray[i]` is already "double enough". – Corak Sep 08 '17 at 13:04
  • Agree. Both of them i could use (I'm not a fan of Magic numbers), but for the sake of simplicity due the OP is a beginner, i tried doing it as straight forward as possible. And for the double concerns, i found it more reasonable to treat the variable `numberOfRolledTimes` as it's true meaning of integer and not forcing it to be double in favor of shorting my code. I stopped trying doing so, when i found out it not contributing. – Orel Eraki Sep 08 '17 at 13:07
  • @OrelEraki - not sure i follow. I thought you used `numberOfRolledTimes*1.0` to be sure to have `double / double`. While `double / int` would already result in a `double`. (The numerator `100.0*numberAndTimesArray[i]` already being `double`, because that's `double * int`, which results in `double`). – Corak Sep 08 '17 at 13:49
  • @Corak, I thought i removed `1.0` from the end, i totally forgot it. – Orel Eraki Sep 08 '17 at 14:03
1

You may Create a List<int> or int[] or Enumerable<int> using a for loop or by simple using Enumerable.Range. given the array only remains randomizing thier order witch you can simply do using .OrderBy and a random number to be ordered by it:

Random rnd = new Random();
int start = 1, end = 6;
int[] results = Enumerable.Range(start, end).OrderBy(x=> rnd.Next()).ToArray();
Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
  • Thank you for the help. If the question is misplaced or .. I'm going to delete it. Because of your downvotes. Didn't have the time to ask the question like it has to be asked. – L.Jov Sep 08 '17 at 12:06
0
var numRollDices = int.Parse(Console.ReadLine());;
var randomGenerator = new Random();

var result = Enumerable
                .Range(0, numRollDices)
                .Select(i => randomGenerator.Next(1, 7))
                .GroupBy(n => n)
                .Select(n => new
                            {
                                Num = n.First(),
                                Count = n.Count(),
                                Percentile = ((n.Count() * 100) / numRollDices)
                            });

Console.WriteLine(string.Join($"{Environment.NewLine}", result.ToList()));
Pollob
  • 29
  • 1
  • 4