C# Dice Program Help!!!

hanleyhansen's Avatar, Join Date: Jan 2008
Pro contributor
I am working on this book that is teaching me C#. I need help with one of the challenges. This is what I have to do: Write a program that simulates a “loaded” die that comes up with the value 1 for half the time and some other random value the other half of the time. All I want is a few hints to get me started.
cadwtr's Avatar, Join Date: Mar 2008
Newbie Member
I'm not sure if this is the simplist way but could make an array with ten members. The first five would be '1', and the rest 2-5. Then run a randomizer to pick an array at random. Maybe something like:

public Random DiceRoller;
string Dice[] = { "1","1","1","1","1","2","3","4","5","6"}
public string RollDice();
{
string DiceValue = Dice[DiceRoller.Next(Dice.Length)];
retrun DiceValue
}

Not sure if this is what you were looking for or if it helps.
hanleyhansen's Avatar, Join Date: Jan 2008
Pro contributor
Thanks i'll try that when I get home.
hanleyhansen's Avatar, Join Date: Jan 2008
Pro contributor
I found a better way but thanks for your help. What I did was influence the array for the random instead because you can manipulate it much better. Your idea works but I find this one to be more elegant and easier to read. Thank you anyway.
Dormidont's Avatar, Join Date: Jan 2011
Newbie Member
Code:
//Write a program that simulates a "loaded" die that comes up with the value 1 for half the time
//and some other random value the other half the time.

//This is an exercise from Chapter 2 of book "Microsoft C# programming for the absolute beginner".
//This example is in no way the most elegant solution. It was written strictly for illustrative 
//purpose.
using System;

namespace LoadedDieProblemChapter2
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Rolling the loaded die:");
            LoadedDie loaded = new LoadedDie();
            Console.WriteLine(loaded);
            Console.WriteLine("\nRolling the normal die:");
            NormalDie normal = new NormalDie();
            Console.WriteLine(normal);
            Console.WriteLine("\nConclusion: try not to play against somebody with a loaded die");

        }
    }
    class LoadedDie
    {
        //Some field data to see how many ones....sixes we get out of a thousand die rolls
        public int ones;
        public int twos;
        public int threes;
        public int fours;
        public int fives;
        public int sixes;
        public int[] dies; //keep the results of die rolls

        public LoadedDie()
        {
            Random r = new Random(); //get Random object
            double raw; //will hold the result of r.NextDouble()
            int die; //will hold the casted raw
            dies = new int[1000];
            for (int i = 0; i < 1000; i++) //roll the die 1000 times
            {
                raw = r.NextDouble(); //get a value in the range [0.0:1.0]
                if (raw <= 0.5) //here is the "loaded" logic. In english: load the die to return a given value approximately half of the time
                {
                    dies[i] = die = 1; //the "loaded" value
                    ones++; //increase the ones count
                }
                else //what to do with the rest half of the time
                {
                    raw = r.NextDouble(); //regenerate random number. If we would use the previous value of raw, we would lose some values
                    die = (int)(raw * 6) + 1; //cast the raw to a value in the range [1:6]
                    dies[i] = die; //store the result
                    switch (die) //see what value die roll returned, adjust corresponding field
                    {
                        case 1: ones++; break;
                        case 2: twos++; break;
                        case 3: threes++; break;
                        case 4: fours++; break;
                        case 5: fives++; break;
                        case 6: sixes++; break;
                    }
                }
            }
        }

        public override string ToString() //override ToString() to make it easy to see the results of the 1000 rolls
        {
            return string.Format("There were {0} rolls\n1's: {1}\n2's: {2}\n3's: {3}\n4's: {4}\n5's: {5}\n6's: {6}",
                dies.Length, ones, twos, threes, fours, fives, sixes);
        }
    }

    class NormalDie
    {
        public int ones;
        public int twos;
        public int threes;
        public int fours;
        public int fives;
        public int sixes;
        public int[] dies;

        public NormalDie()
        {
            Random r = new Random();
            double raw;
            int die;
            dies = new int[1000];
            for (int i = 0; i < 1000; i++)
            {
                raw = r.NextDouble();
                die = (int)(raw * 6) + 1;
                dies[i] = die;
                switch (die)
                {
                    case 1: ones++; break;
                    case 2: twos++; break;
                    case 3: threes++; break;
                    case 4: fours++; break;
                    case 5: fives++; break;
                    case 6: sixes++; break;
                }
            }
        }

        public override string ToString()
        {
            return string.Format("There were {0} rolls\n1's: {1}\n2's: {2}\n3's: {3}\n4's: {4}\n5's: {5}\n6's: {6}",
                dies.Length, ones, twos, threes, fours, fives, sixes);
        }
    }
}

Last edited by shabbir; 16Jan2011 at 12:20.. Reason: Code blocks