Tech Off Post

Single Post Permalink

View Thread: C# dice Roller
  • User profile image

    Or, if you're not planning to eventually do something with that array, you could just do it this way. You could seed the random class with something more exotic (if you really care, if you're an online casino then you *really* care).


      using System;
      using System.Text;

      public class EntryPoint {

        public static void Main(string[] args) {

          Console.WriteLine(Dice.Roll(2, 6));
          Console.WriteLine(Dice.Roll(4,Eye Rolling);
          Console.Write("Press ENTER to exit.");


      public class Dice {

        public static String Roll(Int32 numberOfDice, Int32 numberOfSides) {

          if (numberOfDice <= 0) {
            throw new ApplicationException("Number of die must be greater than zero.");

          if (numberOfSides <= 0) {
            throw new ApplicationException("Number of sides must be greater than zero.");

          Random rnd = new Random((Int32)DateTime.Now.Ticks);
          StringBuilder result = new StringBuilder();
          Int32 total = 0;

          for (Int32 i = 0; i < numberOfDice; i++) {

            Int32 roll = rnd.Next(1, numberOfSides);
            total += roll;
            result.AppendFormat("Dice {0:00}:\t{1}\n", i + 1, roll);


          result.AppendFormat("TOTAL:\t\t{0}\n", total);

          return result.ToString();