This is my 2 die roll code
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 integer for randGen
SimulateRolls(numRolls);
}
private void SimulateRolls(int iNumRolls)
{
int numrolls = int.Parse(txtNumRolls.Text);
Random randGen = new Random();
// randGen is the new random so we use randGen.Next();
int oneRoll = randGen.Next(1, 7) + randGen.Next(1, 7);
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);
int[] arrNumOccurrence = new int[13];
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]++;
}
string rolls = "";
for (int i = 1; i < numrolls; i++)
{
Random random = new Random(1);
i = random.Next(1, 7) + random.Next(1, 7);
rolls = rolls + i;
}
MessageBox.Show(rolls + "\t" + rolls );
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
}
private void label1_Click(object sender, EventArgs e)
{
}
private void diceRoll_Load(object sender, EventArgs e)
{
}
}
}
As you can see there is a lot of stupid stuff in it. The only thing I can’t do is make the random number generator occur, using one name, the number of times the user wants. This is a Windows Forms application.
You can see that I called them oneRoll and twoRoll and so on so I get different values but I want oneRoll to occur as many times as a user wants and to give different numbers.
Right – so you need a collection of some form. For example:
Then do whatever else you need to in a similar way, looping. You may not even need to store all the rolls – if you’re just incrementing the occurences, you could use:
Basically it’s not clear exactly what your assignment is, but a mixture of collections and for loops is likely to get you on the right track.