I am trying to make a program in which the user is prompted to input a number(max length of 4 digits), and then the program shall produce the number of outcomes of a two dice roll meaning that 1 roll includes two die. The outcomes could be 2-12 and I also have to show how many times the number occurs and the percent the numbers occurs. It should be a bell curve meaning that if a user types 1000 rolls, 7 should have the most out comes and then 6 and 8 and so on. I got part of the program going using my proctors help but the help made me more confused could any body help me?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

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

        private void rollDice_Click(object sender, EventArgs e)
        {
            AcceptButton = rollDice;
            if (numRolls.Text.Trim() == "")
            {
                MessageBox.Show("Must enter number of dice rolls");
            }
            else if (numRolls.Text.Trim() == "0")
            {
                MessageBox.Show("Must enter a number greatre than 0");
            }
            int iRolls = int.Parse(numRolls.Text);
            // this convert the number of rolls the person wants and assigns it to iRolls
            Random randGen = new Random(iRolls);
            int iDiceTotal = randGen.Next(1, 6) + randGen.Next(1, 6);
            // this will add the two random values to be the equivalent of two dice
            int[] numOccurrence = new int[11] {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
            // this is an array that has the number of dice there are in
            numOccurrence[iDiceTotal]++;
            StringBuilder strMsg;
            strMsg = new StringBuilder("# of times\n");
            for (int i = 2; i <= 12; i++)
            {
                strMsg.Append(numOccurrence[i].ToString() + "\n");
            }
        }
    }
}

Edited 3 Years Ago by Dani: Formatting fixed

Please use code tags. To do this, put [ c o d e ] [ / c o d e ] around your code. It makes it easier for us to read.

Seems like you need to put that randomizing code into a loop...

int[] numOccurrence = new int[12];

for (int i = 0; i < iRollcount; ++i)
    ++numOccurence[randGen.Next(1, 6) + randGen.Next(1, 6)];

double[] percentOccurence = new double[12];
for (int i = 0; i < 12; ++i)
    percentOccurence[i] = ((double)numOccurence[i] / (double)iRollCount) * 100d

It makes a little bit more sense to put your occurence count in a dictionary:

Dictionary<int, int> numOccurence = new Dictionary<int, int>();
Dictionary<int, double> percentOccurence = new Dictionary<int, double>();

for (int i = 0; i < iRollcount; ++i)
     ++numOccurence[randGen.Next(1, 6) + randGen.Next(1, 6)];
foreach (int iNum in numOccurence.Keys)
    percentOccurence[iNum] = ((double)numOccurence[iNum] / (double)iRollCount) * 100d

This way you arent wasting the 0 or 1 index for a value that will never happen. Basically a dictionary maps a key object to a value object, the object types defined in the declaration.

Edited 5 Years Ago by skatamatic: Figuired I would mention dictionaries

Comments
Thanks that helped

Could be wrong here , but unless a dice has only 5 sides it should be randGen.Next(1, 7) instead of randGen.Next(1, 6)

Could be wrong here , but unless a dice has only 5 sides it should be randGen.Next(1, 7) instead of randGen.Next(1, 6)

That's right. The lower bound (the first number) is inclusive, while the upper bound is exclusive. Just another nuance to make programmers pull their hair out I guess.

This article has been dead for over six months. Start a new discussion instead.