I was making a windows form project. in the project the user inputs the number of dice rolls he wants(2 dice will be rolled) and then i am suppose to give the number of times each number happened and the percent of times each number happened. The only problem is that i cant make the random number generator generator more than one roll using one name.

namespace Dice_Roll_assignment
{
    public partial class diceRoll : Form
    {
        public diceRoll()
        {
            InitializeComponent();
        }

        private void rollDice_Click(object sender, EventArgs e)
        {
            AcceptButton = rollDice;
            if (txtNumRolls.Text.Trim() == "")
            {
                MessageBox.Show("Must enter number of dice rolls");
            }
            else if (txtNumRolls.Text.Trim() == "0")
            {
                MessageBox.Show("Must enter a number greater than 0");
            }
            diceTotal.Text = "Dice Total" + "\n" + 2 + "\n" + 3 + "\n" + 4 + "\n" + 5 + "\n" + 6 + "\n" + 7 + "\n" + 8 + "\n" + 9 + "\n" + 10 + "\n" + 11 + "\n" + 12;
            // This will lead the first label to show the number of dice total
            
            int numRolls = int.Parse(txtNumRolls.Text);// Converting the textBox value to an interger for randGen
            SimulateRolls(numRolls);           
        }
        private void SimulateRolls(int iNumRolls)
        {
            int numrolls = int.Parse(txtNumRolls.Text);
            Random randGen = new Random(numrolls);
            int[] arrNumOccurrence = new int[13];
            // randGen is the new random so we use randGen.Next();
            int oneRoll = randGen.Next(1, 7) + randGen.Next(1, 7);
       // i want this to occur the number of times inputed
            int twoRoll = randGen.Next(1, 7) + randGen.Next(1, 7);
            int threeRoll = randGen.Next(1, 7) + randGen.Next(1, 7);
            int fourRoll = randGen.Next(1, 7) + randGen.Next(1, 7);
            int fiveRoll = randGen.Next(1, 7) + randGen.Next(1, 7);
            int sixRoll = randGen.Next(1, 7) + randGen.Next(1, 7);
            int sevenRoll = randGen.Next(1, 7) + randGen.Next(1, 7);
            int eightRoll = randGen.Next(1, 7) + randGen.Next(1, 7);
            int nineRoll = randGen.Next(1, 7) + randGen.Next(1, 7);
            int tenRoll = randGen.Next(1, 7) + randGen.Next(1, 7);
            int elevenRoll = randGen.Next(1, 7) + randGen.Next(1, 7);
            int twelveRoll = randGen.Next(1, 7) + randGen.Next(1, 7);
            int thirteenRoll = randGen.Next(1, 7) + randGen.Next(1, 7);
            int fourteenRoll = randGen.Next(1, 7) + randGen.Next(1, 7);
            if (numrolls == 1)
            {
                arrNumOccurrence[oneRoll]++;
            }
            else if (numrolls == 2)
            {
                arrNumOccurrence[oneRoll]++;
                arrNumOccurrence[twoRoll]++;
            }
            else if (numrolls  ==3)
            {
                arrNumOccurrence[oneRoll]++;
                arrNumOccurrence[twoRoll]++;
                arrNumOccurrence[threeRoll]++;
            }
            else if (numrolls  == 4)
            {
                arrNumOccurrence[oneRoll]++;
                arrNumOccurrence[twoRoll]++;
                arrNumOccurrence[threeRoll]++;
                arrNumOccurrence[fourRoll]++;
            }
            else if (numrolls  == 5)
            {
                arrNumOccurrence[oneRoll]++;
                arrNumOccurrence[twoRoll]++;
                arrNumOccurrence[threeRoll]++;
                arrNumOccurrence[fourRoll]++;
                arrNumOccurrence[fiveRoll]++;
            }
            else if (numrolls  == 6)
            {
                arrNumOccurrence[oneRoll]++;
                arrNumOccurrence[twoRoll]++;
                arrNumOccurrence[threeRoll]++;
                arrNumOccurrence[fourRoll]++;
                arrNumOccurrence[fiveRoll]++;
                arrNumOccurrence[sixRoll]++;
            }
            else if (numrolls  == 7)
            {
                arrNumOccurrence[oneRoll]++;
                arrNumOccurrence[twoRoll]++;
                arrNumOccurrence[threeRoll]++;
                arrNumOccurrence[fourRoll]++;
                arrNumOccurrence[fiveRoll]++;
                arrNumOccurrence[sixRoll]++;
                arrNumOccurrence[sevenRoll]++;
            }
            else if (numrolls  == 8)
            {
                arrNumOccurrence[oneRoll]++;
                arrNumOccurrence[twoRoll]++;
                arrNumOccurrence[threeRoll]++;
                arrNumOccurrence[fourRoll]++;
                arrNumOccurrence[fiveRoll]++;
                arrNumOccurrence[sixRoll]++;
                arrNumOccurrence[sevenRoll]++;
                arrNumOccurrence[eightRoll]++;
            }
            else if (numrolls  == 9)
            {
                arrNumOccurrence[oneRoll]++;
                arrNumOccurrence[twoRoll]++;
                arrNumOccurrence[threeRoll]++;
                arrNumOccurrence[fourRoll]++;
                arrNumOccurrence[fiveRoll]++;
                arrNumOccurrence[sixRoll]++;
                arrNumOccurrence[sevenRoll]++;
                arrNumOccurrence[eightRoll]++;
                arrNumOccurrence[nineRoll]++;
            }
            else if (numrolls  == 10)
            {
                arrNumOccurrence[oneRoll]++;
                arrNumOccurrence[twoRoll]++;
                arrNumOccurrence[threeRoll]++;
                arrNumOccurrence[fourRoll]++;
                arrNumOccurrence[fiveRoll]++;
                arrNumOccurrence[sixRoll]++;
                arrNumOccurrence[sevenRoll]++;
                arrNumOccurrence[eightRoll]++;
                arrNumOccurrence[nineRoll]++;
                arrNumOccurrence[tenRoll]++;
            }
            else if (numrolls  == 11)
            {
                arrNumOccurrence[oneRoll]++;
                arrNumOccurrence[twoRoll]++;
                arrNumOccurrence[threeRoll]++;
                arrNumOccurrence[fourRoll]++;
                arrNumOccurrence[fiveRoll]++;
                arrNumOccurrence[sixRoll]++;
                arrNumOccurrence[sevenRoll]++;
                arrNumOccurrence[eightRoll]++;
                arrNumOccurrence[nineRoll]++;
                arrNumOccurrence[tenRoll]++;
                arrNumOccurrence[elevenRoll]++;
            }
            else if (numrolls  == 12)
            {
                arrNumOccurrence[oneRoll]++;
                arrNumOccurrence[twoRoll]++;
                arrNumOccurrence[threeRoll]++;
                arrNumOccurrence[fourRoll]++;
                arrNumOccurrence[fiveRoll]++;
                arrNumOccurrence[sixRoll]++;
                arrNumOccurrence[sevenRoll]++;
                arrNumOccurrence[eightRoll]++;
                arrNumOccurrence[nineRoll]++;
                arrNumOccurrence[tenRoll]++;
                arrNumOccurrence[elevenRoll]++;
                arrNumOccurrence[twelveRoll]++;
            }
            else if (numrolls == 13)
            {
                arrNumOccurrence[oneRoll]++;
                arrNumOccurrence[twoRoll]++;
                arrNumOccurrence[threeRoll]++;
                arrNumOccurrence[fourRoll]++;
                arrNumOccurrence[fiveRoll]++;
                arrNumOccurrence[sixRoll]++;
                arrNumOccurrence[sevenRoll]++;
                arrNumOccurrence[eightRoll]++;
                arrNumOccurrence[nineRoll]++;
                arrNumOccurrence[tenRoll]++;
                arrNumOccurrence[elevenRoll]++;
                arrNumOccurrence[twelveRoll]++;
                arrNumOccurrence[thirteenRoll]++;
            }
            else if (numrolls == 14)
            {
                arrNumOccurrence[oneRoll]++;
                arrNumOccurrence[twoRoll]++;
                arrNumOccurrence[threeRoll]++;
                arrNumOccurrence[fourRoll]++;
                arrNumOccurrence[fiveRoll]++;
                arrNumOccurrence[sixRoll]++;
                arrNumOccurrence[sevenRoll]++;
                arrNumOccurrence[eightRoll]++;
                arrNumOccurrence[nineRoll]++;
                arrNumOccurrence[tenRoll]++;
                arrNumOccurrence[elevenRoll]++;
                arrNumOccurrence[twelveRoll]++;
                arrNumOccurrence[thirteenRoll]++;
                arrNumOccurrence[fourteenRoll]++;
            }
            numTimes.Text = "# of Times\n" + arrNumOccurrence[2]++ + "\n" + arrNumOccurrence[3]++ + "\n" + arrNumOccurrence[4]++ + "\n" + arrNumOccurrence[5]++ + "\n" + arrNumOccurrence[6]++ + "\n" + arrNumOccurrence[7]++ + "\n" + arrNumOccurrence[8]++ + "\n" + arrNumOccurrence[9]++ + "\n" + arrNumOccurrence[10]++ + "\n" + arrNumOccurrence[11]++ + "\n" + arrNumOccurrence[12]++ + "\n";
            // Now This tallys the thing that happens only once
        }

Of course it only happens one time, you have no loop construct in your code to make it happen more than once (no for, no foreach, no while or no do/while).

And that is some really ugly code. Do you know what an array is? Step back a bit, learn what an array is, learn how to loop and learn how to reuse code. There is no need for fourteen different roll variables. What are you going to do if the user wants a million rolls?

If I was you, I would go back and start the project from scratch again with a different approach.

However be sure to use arrays the size of the "numrolls" and then use a foreach.

int[] numrollsArray = new int[numrolls];
foreach (int i in numrollsArray) 
{
}

This is my code now but i can't get the percentages of the numbers.

namespace Dice_Roll_assignment
{
    public partial class diceRoll : Form
    {
        public diceRoll()
        {
            InitializeComponent();
        }

        private void rollDice_Click(object sender, EventArgs e)
        {
            AcceptButton = rollDice;
            if (txtNumRolls.Text.Trim() == "")
            {
                MessageBox.Show("Must enter number of dice rolls");
                return;
            }
            else if (txtNumRolls.Text.Trim() == "0")
            {
                MessageBox.Show("Must enter a number greater than 0");
                return;
            }
            diceTotal.Text = "Dice Total" + "\n" + 2 + "\n" + 3 + "\n" + 4 + "\n" + 5 + "\n" + 6 + "\n" + 7 + "\n" + 8 + "\n" + 9 + "\n" + 10 + "\n" + 11 + "\n" + 12;
            // This will lead the first label to show the number of dice total
            
            int numRolls = int.Parse(txtNumRolls.Text);// Converting the textBox value to an interger for randGen
            SimulateRolls(numRolls);           
        }
        private void SimulateRolls(int iNumRolls)
        {
            int numrolls = int.Parse(txtNumRolls.Text);
            Random randGen = new Random(numrolls);
            int[] arrNumOccurrence = new int[13];
            // randGen is the new random so we use randGen.Next();
            for (int i = 0; i < numrolls; i++)
            {
                int iDicetotal = randGen.Next(1, 7) + randGen.Next(1, 7);
                arrNumOccurrence[iDicetotal]++;
            }
            numTimes.Text = "# of Times\n" + arrNumOccurrence[2]++ + "\n" + arrNumOccurrence[3]++ + "\n" + arrNumOccurrence[4]++ + "\n" + arrNumOccurrence[5]++ + "\n" + arrNumOccurrence[6]++ + "\n" + arrNumOccurrence[7]++ + "\n" + arrNumOccurrence[8]++ + "\n" + arrNumOccurrence[9]++ + "\n" + arrNumOccurrence[10]++ + "\n" + arrNumOccurrence[11]++ + "\n" + arrNumOccurrence[12]++;
            decimal two = ((arrNumOccurrence[2]++ / numrolls) * 100);
            decimal three = ((arrNumOccurrence[3]++ / numrolls) * 100);
            decimal four = ((arrNumOccurrence[4]++ / numrolls) * 100);
            decimal five = ((arrNumOccurrence[5]++ / numrolls) * 100);
            decimal six = ((arrNumOccurrence[6]++ / numrolls) * 100);
            decimal seven = ((arrNumOccurrence[7]++ / numrolls) * 100);
            decimal eight = ((arrNumOccurrence[8]++ / numrolls) * 100);
            decimal nine = ((arrNumOccurrence[9]++ / numrolls) * 100);
            decimal ten = ((arrNumOccurrence[10]++ / numrolls) * 100);
            decimal eleven = ((arrNumOccurrence[11]++ / numrolls) * 100);
            decimal twelve = ((arrNumOccurrence[12]++ / numrolls) * 100);
            for (int i = 2; i <= 12; i++)
                String.Format("{0:##.00%}\n", (float)arrNumOccurrence[i] / (float)iNumRolls); //help here

            perTimes.Text = "% of Times\n" + two + "\n" + three + "\n" + four + "\n" + five + "\n" + six + "\n" + seven + "\n" + eight + "\n" + nine + "\n" + ten + "\n" + eleven + "\n" + twelve;
        }
    }
}

How do I get percentages on my program. I posted my program on my cousins account(vishal1949) I dont know how to get the percentages.

using System;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1 {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e) {
            int count;

            if (Int32.TryParse(textBox1.Text, out count)) {
                if (count < 1) {
                    MessageBox.Show("Number of rolls must be greater than zero", "Zero Rolls", MessageBoxButtons.OK, MessageBoxIcon.Error);
                } else {
                    int[] values = DoRolls(count);

                    StringBuilder sbValue = new StringBuilder();
                    StringBuilder sbCount = new StringBuilder();
                    StringBuilder sbPrcnt = new StringBuilder();

                    sbValue.Append("Value\n");
                    sbCount.Append("Count\n");
                    sbPrcnt.Append("Percent\n");

                    for (int i = 2; i <= 12; i++) {
                        sbValue.Append(String.Format(" {0:#0}\n", i));
                        sbCount.Append(String.Format("{0:#,##0}\n", values[i]));
                        sbPrcnt.Append(String.Format("{0:##0.00%}\n", ((double)values[i]) / count));
                    }

                    label1.Text = sbValue.ToString();
                    label2.Text = sbCount.ToString();
                    label3.Text = sbPrcnt.ToString();
                }
            } else {
                MessageBox.Show("You must enter a number!", "Not a number", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private static int[] DoRolls(int number) {
            Random r = new Random();
            int[] values = new int[13];

            for (int i = 0; i < number; i++) {
                int total = r.Next(6) + r.Next(6) + 2;
                values[total]++;
            }

            return values;
        }
    }
}

I got the program and the percentages. Thanks for every body's help. It's much cleaner and smaller.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.