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:
List<int> rolls = new List<int>();
for (int i = 0; i < desiredRolls; i++)
{
rolls.Add(randGen.Next(1, 7) + randGen.Next(1, 7));
}
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:
for (int i = 0; i < desiredRolls; i++)
{
int roll = randGen.Next(1, 7) + randGen.Next(1, 7);
arrNumOccurrence[roll]++;
}
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.
You're initializing the Random number generator with the "Seed" 1 each time. Don't do that. That guarantees the sequence will always be the same. Initialize the generator once, before the loop:
Random random = new Random();
for (int i = 1; i < numrolls; i++)
{
i = random.Next(1, 7) + random.Next(1, 7);
rolls = rolls + i;
}
It sounds like you're trying to build a frequency table showing the frequency of particular dice rolls (e.g., theoretically, over the long haul each value of the die should occur pretty uniformly. Something like the following should do:
- Figure out how many sides each die has (not all dice are 6-sided).
- Figure out how many dice you want to cast simultaneously.
- The domain (set of possible values) returned by each cast of the dice is a function of the number of dice cast and the number of sides on each die. For instance, if you cast 2 6-sided dice, the domain of the result is an integer in the range 2–12 inclusive, since a 6-sided die has pips 1-6 on its faces. With something like a 10-sided die, the range is usually 0-9 (just to confuse things), so the domain of a casting 2 10-sided dice is an integer in the range 0-99 inclusive.
- Figure out the number of iterations desired. You might call it sample size: how many times are you going to cast the dice to test your hypothesis?
- As @Jon Skeet pointed out, you need a collection of some sort in order to accumulate the frequency counts. A keyed collection of some sort is what you need, where the key of the collection is the value of the dice roll, and the value is the frequency — the number of times that particular dice roll occurred. The the size of the collection is thus dependent upon the domain of the cast, obviously. Note that "keyed collection" doesn't necessarily mean "dictionary" or "hash table": an array is [informally] keyed, the index of the array element serving as the key value.
- for each iteration specified, cast the dice and tick up the frequency count for that particular result by 1.
Easy!
Edited to note: You only need a single source of entropy (random number generator).
精彩评论