在 C# 中创建骰子出现的频率 table
Creating a frequency table of dice occurrences in C#
我是编程新手,目前正在学习 C#。
我有两个随机掷出的骰子,共掷 50 次
将两个骰子的总和加在一起得出每次掷骰的总和。
我的问题是,如何创建一个频率 table 来计算在 50 次掷骰结束时掷出某个数字的次数。
以下是我想要实现的目标。掷出的每个数字的理货图表(来自两个骰子)
1: |||
2: ||||
3: ||||||
4: |||||
5: ||||||||
6: ||
到目前为止,这是我的代码,
class Program
{
const int DICE_ROLLS = 51;
static void Main(string[] args)
{
Random random = new Random();
int[] firstDice = new int[DICE_ROLLS];
int[] secondDice = new int[DICE_ROLLS];
int diceSum = 0;
for (int roll = 1; roll <= 50; roll++)
{
firstDice[roll] = GenerateNumber(random);
secondDice[roll] = GenerateNumber(random);
diceSum = firstDice[roll] + secondDice[roll];
Console.WriteLine("ROLL {0}: {1} + {2} = {3}", roll, firstDice[roll], secondDice[roll], diceSum);
}
Console.WriteLine();
Console.WriteLine("~~~~~~~~~~~~~~~~");
Console.WriteLine("Number Frequency");
}
static int GenerateNumber (Random random)
{
return random.Next(1, 7);
}
}
试试这个:
const int DICE_ROLLS = 51;
static void Main(string[] args)
{
Random random = new Random();
int[] firstDice = new int[DICE_ROLLS];
int[] secondDice = new int[DICE_ROLLS];
int[] count = new int[6];
int diceSum = 0;
for (int roll = 0; roll <= DICE_ROLLS - 1; roll++)
{
firstDice[roll] = GenerateNumber(random);
secondDice[roll] = GenerateNumber(random);
diceSum = firstDice[roll] + secondDice[roll];
Console.WriteLine("ROLL {0}: {1} + {2} = {3}", roll + 1, firstDice[roll], secondDice[roll], diceSum);
}
Console.WriteLine();
Console.WriteLine("~~~~~~~~~~~~~~~~");
Console.WriteLine("Number Frequency");
var allRolls = firstDice.Concat(secondDice).ToList();
for(var i = 1; i <= 6; i++)
{
Console.Write(i + ": ");
for(var j = 0; j < allRolls.Count(c => c == i); j++)
Console.Write("|");
Console.WriteLine();
}
}
static int GenerateNumber (Random random)
{
return random.Next(1, 7);
}
给出:
~~~~~~~~~~~~~~~~
Number Frequency
1: ||||||||||||||||||||
2: |||||||||||
3: ||||||||||||||||||
4: ||||||||||||||||
5: |||||||||||||||||||||
6: ||||||||||||||||
假设有 6 个面的骰子,Dictionary
可能是一个不错的选择。
var rolls = new SortedDictionary<int, int> { {1,0}, {2,0}, {3,0}, {4,0}, {5,0}, {6,0} };
for(int i = 0; i < 50; i++)
{
var first = Random.Next(1, 7);
var second = Random.Next(1, 7);
rolls[first]++;
rolls[second]++;
// Rest of rolling display code
}
// Header display code
foreach(var roll in rolls)
{
Console.Write("{0}: ", roll.Key);
for(int i = 0; i < rolls.Value; i++)
{
Console.Write("|");
}
Console.WriteLine();
}
您甚至可以根据像
这样的变量生成初始字典值,从而根据不同面的骰子轻松自定义它
int sides = 12;
var rolls = new SortedDictionary<int, int>();
foreach(var value in Enumerable.Range(1, sides))
{
rolls.Add(value, 0);
}
您可以将 Random.Next(1, 7)
替换为 Random.Next(1, sides)
,并有一个非常通用的程序来掷骰子并计算它们的频率。
这个有效:
var rnd = new Random();
Console.WriteLine(
String.Join(
Environment.NewLine,
Enumerable
.Range(0, 50)
.Select(n => rnd.Next(1, 7))
.ToLookup(x => x)
.Select((xs, n) =>
String.Format("{0}: {1}", n + 1, new String('|', xs.Count())))));
我明白了(例如):
1: |||||||
2: |||||||
3: |||||||||||
4: |||||||||
5: ||||||||
6: ||||||||
我是编程新手,目前正在学习 C#。
我有两个随机掷出的骰子,共掷 50 次 将两个骰子的总和加在一起得出每次掷骰的总和。
我的问题是,如何创建一个频率 table 来计算在 50 次掷骰结束时掷出某个数字的次数。
以下是我想要实现的目标。掷出的每个数字的理货图表(来自两个骰子)
1: |||
2: ||||
3: ||||||
4: |||||
5: ||||||||
6: ||
到目前为止,这是我的代码,
class Program
{
const int DICE_ROLLS = 51;
static void Main(string[] args)
{
Random random = new Random();
int[] firstDice = new int[DICE_ROLLS];
int[] secondDice = new int[DICE_ROLLS];
int diceSum = 0;
for (int roll = 1; roll <= 50; roll++)
{
firstDice[roll] = GenerateNumber(random);
secondDice[roll] = GenerateNumber(random);
diceSum = firstDice[roll] + secondDice[roll];
Console.WriteLine("ROLL {0}: {1} + {2} = {3}", roll, firstDice[roll], secondDice[roll], diceSum);
}
Console.WriteLine();
Console.WriteLine("~~~~~~~~~~~~~~~~");
Console.WriteLine("Number Frequency");
}
static int GenerateNumber (Random random)
{
return random.Next(1, 7);
}
}
试试这个:
const int DICE_ROLLS = 51;
static void Main(string[] args)
{
Random random = new Random();
int[] firstDice = new int[DICE_ROLLS];
int[] secondDice = new int[DICE_ROLLS];
int[] count = new int[6];
int diceSum = 0;
for (int roll = 0; roll <= DICE_ROLLS - 1; roll++)
{
firstDice[roll] = GenerateNumber(random);
secondDice[roll] = GenerateNumber(random);
diceSum = firstDice[roll] + secondDice[roll];
Console.WriteLine("ROLL {0}: {1} + {2} = {3}", roll + 1, firstDice[roll], secondDice[roll], diceSum);
}
Console.WriteLine();
Console.WriteLine("~~~~~~~~~~~~~~~~");
Console.WriteLine("Number Frequency");
var allRolls = firstDice.Concat(secondDice).ToList();
for(var i = 1; i <= 6; i++)
{
Console.Write(i + ": ");
for(var j = 0; j < allRolls.Count(c => c == i); j++)
Console.Write("|");
Console.WriteLine();
}
}
static int GenerateNumber (Random random)
{
return random.Next(1, 7);
}
给出:
~~~~~~~~~~~~~~~~
Number Frequency
1: ||||||||||||||||||||
2: |||||||||||
3: ||||||||||||||||||
4: ||||||||||||||||
5: |||||||||||||||||||||
6: ||||||||||||||||
假设有 6 个面的骰子,Dictionary
可能是一个不错的选择。
var rolls = new SortedDictionary<int, int> { {1,0}, {2,0}, {3,0}, {4,0}, {5,0}, {6,0} };
for(int i = 0; i < 50; i++)
{
var first = Random.Next(1, 7);
var second = Random.Next(1, 7);
rolls[first]++;
rolls[second]++;
// Rest of rolling display code
}
// Header display code
foreach(var roll in rolls)
{
Console.Write("{0}: ", roll.Key);
for(int i = 0; i < rolls.Value; i++)
{
Console.Write("|");
}
Console.WriteLine();
}
您甚至可以根据像
这样的变量生成初始字典值,从而根据不同面的骰子轻松自定义它int sides = 12;
var rolls = new SortedDictionary<int, int>();
foreach(var value in Enumerable.Range(1, sides))
{
rolls.Add(value, 0);
}
您可以将 Random.Next(1, 7)
替换为 Random.Next(1, sides)
,并有一个非常通用的程序来掷骰子并计算它们的频率。
这个有效:
var rnd = new Random();
Console.WriteLine(
String.Join(
Environment.NewLine,
Enumerable
.Range(0, 50)
.Select(n => rnd.Next(1, 7))
.ToLookup(x => x)
.Select((xs, n) =>
String.Format("{0}: {1}", n + 1, new String('|', xs.Count())))));
我明白了(例如):
1: |||||||
2: |||||||
3: |||||||||||
4: |||||||||
5: ||||||||
6: ||||||||