C# 计算掷骰子的频率
我为一个C#windows窗体项目编写了一个事件处理程序,它模拟了100个两个骰子的掷骰,并返回每个掷骰子的两个骰子的总和。代码对每一个和都做三件事,它将它放在一个名为“rollDisplay”的RichTextBox中,它将和写入一个文本文件,并将和添加到一个名为“rolls”的整数列表中。所有这些都很好,但现在我需要计算每个总和的频率,并在列表框中显示这些频率。正如下面的代码所示,我正在使用11个不同的计数器变量和11个不同的字符串添加到列表框中。我下面的方法很管用,但它真的很难看,在我看来,这是一种非常低效的方法 我的问题是。有人能建议一种更干净的方法吗?作业要求我有11个单独的计数器来识别掷骰子的频率,所以我不能像我在其他几个问题中看到的那样使用Linq,但我真的很不确定这是实现我所需要的最好方法C# 计算掷骰子的频率,c#,C#,我为一个C#windows窗体项目编写了一个事件处理程序,它模拟了100个两个骰子的掷骰,并返回每个掷骰子的两个骰子的总和。代码对每一个和都做三件事,它将它放在一个名为“rollDisplay”的RichTextBox中,它将和写入一个文本文件,并将和添加到一个名为“rolls”的整数列表中。所有这些都很好,但现在我需要计算每个总和的频率,并在列表框中显示这些频率。正如下面的代码所示,我正在使用11个不同的计数器变量和11个不同的字符串添加到列表框中。我下面的方法很管用,但它真的很难看,在我看来
private void Roll_Click(object sender, EventArgs e)
{
int roll = 0, hitTwo = 0, hitThree = 0, hitFour = 0, hitFive = 0, hitSix = 0, hitSeven = 0, hitEight = 0,
hitNine = 0, hitTen = 0, hitEleven = 0, hitTwelve = 0;
String freq1, freq2, freq3, freq4, freq5, freq6, freq7, freq8, freq9, freq10, freq11;
StreamWriter file = new StreamWriter("dicerolls.txt", true);
List<int> rolls = new List<int>();
for (int i = 0; i < 100; i++)
{
dieUno.setSide(dieUno.roll());
dieDuo.setSide(dieDuo.roll());
int diceTotal = dieUno.getSide() + dieDuo.getSide();
rolls.Add(diceTotal);
rollDisplay.AppendText(diceTotal.ToString() + "\u2028");
try
{
file.WriteLine(diceTotal.ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
file.Close();
for (int i = 0; i < rolls.Count; i++)
{
roll = rolls[i];
if(roll==2)
{ hitTwo++; }
else if(roll==3)
{ hitThree++; }
else if(roll==4)
{ hitFour++; }
else if(roll==5)
{ hitFive++; }
else if(roll==6)
{ hitSix++; }
else if(roll==7)
{ hitSeven++; }
else if(roll==8)
{ hitEight++; }
else if(roll==9)
{ hitNine++; }
else if (roll == 10)
{ hitTen++; }
else if (roll == 11)
{ hitEleven++; }
else if (roll == 12)
{ hitTwelve++; }
}
freq1 = " 2 Occurs: " + hitTwo + " times"; freq2 = " 3 Occurs: " + hitThree + " times"; freq3 = " 4 Occurs: " + hitFour + " times";
freq4 = " 5 Occurs: " + hitFive + " times"; freq5 = " 6 Occurs: " + hitSix + " times"; freq6 = " 7 Occurs: " + hitSeven + " times";
freq7 = " 8 Occurs: " + hitEight + " times"; freq8 = " 9 Occurs: " + hitNine + " times"; freq9 = " 10 Occurs: " + hitTen + " times";
freq10 = " 11 Occurs: " + hitEleven + " times"; freq11 = " 12 Occurs: " + hitTwelve + " times";
frequency.Items.Add(freq1); frequency.Items.Add(freq2); frequency.Items.Add(freq3); frequency.Items.Add(freq4); frequency.Items.Add(freq5);
frequency.Items.Add(freq6); frequency.Items.Add(freq7); frequency.Items.Add(freq8); frequency.Items.Add(freq9); frequency.Items.Add(freq10);
frequency.Items.Add(freq11);
}
private void Roll\u单击(对象发送者,事件参数e)
{
int roll=0,hit2=0,hitThree=0,hitFour=0,hitFive=0,hitSix=0,hitSeven=0,hitEight=0,
打九等于0,打十等于0,打十一等于0,打十二等于0;
字符串freq1、freq2、freq3、freq4、freq5、freq6、freq7、freq8、freq9、freq10、freq11;
StreamWriter file=newstreamwriter(“dicerolls.txt”,true);
列表滚动=新列表();
对于(int i=0;i<100;i++)
{
dieUno.setSide(dieUno.roll());
diedo.setSide(diedo.roll());
int diceTotal=dieUno.getSide()+diedou.getSide();
掷骰子。加上(总共);
rollDisplay.AppendText(diceTotal.ToString()+“\u2028”);
尝试
{
file.WriteLine(diceTotal.ToString());
}
捕获(例外情况除外)
{
MessageBox.Show(例如Message);
}
}
file.Close();
对于(int i=0;i
Roll\u单击
做的工作太多了
首先,创建一个GatherSamples
函数
int[] GatherSamples()
{
var rolls = new List<int>();
// roll dice ...
return rolls.ToArray();
}
。。。以及WriteToDisk
方法
void DisplayRolls(int[] rolls)
{
// output to your control
}
void WriteToDisk(int[] rolls, string file)
{
using (var writer = new StreamWriter(file)) // research C# using if you don't understand this
{
...
}
}
。。。然后做频率分析
string[] AnalyzeRolls(int[] rolls)
{
// though I don't approve of 11 counters, use them here
return new [] { "2 occurs " ... };
}
。。。那么就这样称呼它:
foreach(var analysis in AnalyzeRolls(rolls))
{
frequency.Items.Add(analysis);
}
给你。一个6面骰子辊。它对用户界面一无所知。它几乎没有公开其内部实现
class SixSidedDiceRoller
{
private static readonly RandomNumberGenerator rng = RandomNumberGenerator.Create();
private SortedDictionary<int,int> frequencyTable;
private List<Tuple<int,int,int>> rollHistory;
public int Count { get { return rollHistory.Count; } }
public IEnumerable<Tuple<int , int , int>> RollHistory
{
get { return rollHistory.AsReadOnly(); }
}
public IEnumerable<Tuple<int,int>> FrequencyTable
{
get
{
return frequencyTable.Select(
x => new Tuple<int,int>(x.Key,x.Value)
) ;
}
}
public SixSidedDiceRoller()
{
rollHistory = new List<Tuple<int , int , int>>();
// initialize the frequency table
for ( int i = 2 ; i <= 12 ; ++i )
{
frequencyTable[i] = 0;
}
return;
}
public int RollDice()
{
int d1 = RollDie();
int d2 = RollDie();
int n = d1 + d2;
rollHistory.Add( new Tuple<int , int , int>( d1 , d2 , n ) );
++frequencyTable[n];
return n;
}
private int RollDie()
{
byte[] octets = new byte[1];
rng.GetBytes( octets );
int die = 1 + ( octets[0] % 6 );
return die;
}
}
class SixSidedDiceRoller
{
私有静态只读RandomNumberGenerator rng=RandomNumberGenerator.Create();
专用分类词典频率表;
私有列表历史;
公共整数计数{get{return rollHistory.Count;}}
公共IEnumerable历史记录
{
获取{return rollHistory.AsReadOnly();}
}
公共IEnumerable频率表
{
得到
{
返回频率表。选择(
x=>新元组(x.Key,x.Value)
) ;
}
}
公共六边形切碎机()
{
rollHistory=新列表();
//初始化频率表
对于(int i=2;我已经研究过“数组”然而?作业要求我有11个独立的计数器
糟糕。现在学校教你什么?值得赞扬的是,我可以很容易地理解你的代码。我不能说我在这里看到的所有代码中都有这样的代码。如果有的话,选择一个案例不是比所有其他案例更有效、更容易阅读吗?代码方面,你知道了吗ve看起来确实有点难看。但就性能而言,它可能会比其他任何东西都快得多。有点过于复杂,构造函数需要两个它不使用的参数,RollDie
方法不会返回统一的结果。您从0..255生成一个随机值,并将模数取6。值1..4更接近l我很可能会以1/43的比例得出5..6。给定100卷2个骰子(200个样本),结果将偏离预期的钟形曲线。