让随机数生成器在c#中出现1000次
本文关键字:1000次 随机数生成器 | 更新日期: 2023-09-27 18:04:03
这是我的2次掷骰子代码
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)
{
}
}
}
你可以看到里面有很多愚蠢的东西。我唯一不能做的是让随机数生成器出现,使用一个名称,即用户想要的次数。这是一个Windows窗体应用程序
你可以看到我把它们叫做oneRoll
和twoRoll
等等所以我得到了不同的值但是我想让oneRoll
出现的次数和用户想要的不同并且给出不同的数字
正确-所以您需要某种形式的集合。例如:
List<int> rolls = new List<int>();
for (int i = 0; i < desiredRolls; i++)
{
rolls.Add(randGen.Next(1, 7) + randGen.Next(1, 7));
}
然后以类似的方式执行所需的其他操作,即循环。您甚至可能不需要存储所有的滚动-如果您只是增加出现次数,您可以使用:
for (int i = 0; i < desiredRolls; i++)
{
int roll = randGen.Next(1, 7) + randGen.Next(1, 7);
arrNumOccurrence[roll]++;
}
基本上不清楚你的赋值是什么,但是集合和for循环的混合可能会让你走上正确的轨道。
每次都用"Seed" 1初始化随机数生成器。不要那样做。这保证了序列总是相同的。在循环之前初始化生成器一次:
Random random = new Random();
for (int i = 1; i < numrolls; i++)
{
i = random.Next(1, 7) + random.Next(1, 7);
rolls = rolls + i;
}
这听起来像是你试图建立一个频率表,显示特定骰子滚动的频率(例如,理论上,从长远来看,骰子的每个值应该均匀地发生)。应该执行以下操作:
- 计算每个骰子有多少面(不是所有骰子都是6面)。
- 计算出你想同时投多少骰子。
- 每次掷骰子返回的域(可能值的集合)是掷出的骰子数和每个骰子上的面数的函数。例如,如果你投掷2个6面骰子,结果的定义域是一个2和12(包括12)范围内的整数,因为6面骰子的点数是1-6。对于像10面骰子这样的东西,范围通常是0-9(只是为了混淆),所以投掷2个10面骰子的域是0-99范围内的整数。
- 计算所需的迭代次数。你可以把它叫做样本量:你要掷多少次骰子来检验你的假设?
- 正如@Jon Skeet指出的那样,你需要某种类型的集合来累积频率计数。您需要的是某种类型的键集合,其中集合的键是掷骰子的值,值是频率—特定骰子掷出的次数。因此,集合的大小显然取决于强制转换的域。注意"键集合"并不一定意味着"字典"或"哈希表":数组是[非正式的]键的,数组元素的索引作为键值。
- 对于指定的每次迭代,投掷骰子并将特定结果的频率计数增加1。
简单!
编辑说明:你只需要一个单一的熵源(随机数生成器)。