Roll a six-sided die 60,000,000 times and save the frequency in an array - CSharp Language Basics

CSharp examples for Language Basics:Array

Description

Roll a six-sided die 60,000,000 times and save the frequency in an array

Demo Code

using System;/*from  w w  w  .  j  a v a2  s. com*/
class RollDie
{
   static void Main()
   {
      var randomNumbers = new Random(); // random-number generator
      var frequency = new int[7]; // array of frequency counters
      // roll die 60,000,000 times; use die value as frequency index
      for (var roll = 1; roll <= 60000000; ++roll)
      {
         ++frequency[randomNumbers.Next(1, 7)];
      }
      Console.WriteLine($"{"Face"}{"Frequency",10}");
      for (var face = 1; face < frequency.Length; ++face){
         Console.WriteLine($"{face,4}{frequency[face],10}");
      }
   }
}

Result


Related Tutorials