骰子滚动问题 [英] dice rolling problem

查看:363
本文介绍了骰子滚动问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我正在做一个模拟滚动一对骰子36,000

次的练习,然后计算并显示每次模拟滚动的次数

可能总和。


由于某种原因,每次我运行模拟时,我的一笔或多笔钱都是零b / b,这是非常不可能的。所以我确定我的代码中存在错误。但是

我不知道它是什么,即使重读了好几次之后也是如此。可以

有人给我一个提示吗?


这是我的代码:


static void SimulateDiceRolls()

{

//当你掷两个骰子时,有11种可能的总和:

// 2,3,...,10, 11,12。定义一个包含13个元素的数组

//其中前两个元素未使用,但第三个元素

//其中index等于2,对应于sum = 2,依此类推。

//确保它们被初始化为0,默认情况下C#默认为

int [] sum = new int [13] ;


//为36,000次,滚动两个骰子,计算总和,

//并增加与该总和相对应的索引。

int currentResult = 0;

for(int numberOfRolls = 1; numberOfRolls< = 36000; ++ numberOfRolls)

{

currentResult = RollTwoDice();

++ sum [currentResult];

}


//打印每次多少次总和出现了多少时间

//到a求和的3个十进制精度

for(int sumIndex = 2; sumIndex< = 12; ++ sumIndex)

{

Console.WriteLine(" {0,5} {1,5} {2:F3}",sumIndex,sum [sumIndex ],

(双)总和[sumIndex] / 36000 * 100);

}

}


//返回两个随机生成的整数之和,从1到6

//包括

static int RollTwoDice()

{

随机r = new Random();

返回r.Next(1,7)+ r.Next(1,7);

}


在此先感谢,

何塞

Hello, I''m doing an exercise to simulate rolling a pair of dice 36,000
times, then count and display how many times the simulation rolls each
possible sum.

For some reason each time I run my simulation, one or more of my sums is
zero, which is highly unlikely. So I''m sure there''s a bug in my code. But
I can''t tell what it is, even after re-reading it several times. Can
someone give me a hint?

Here is my code:

static void SimulateDiceRolls()
{
// When you roll two dice, there are 11 possible sums:
// 2, 3, ..., 10, 11, 12. Define an array of 13 elements
// where the first two elements are unused, but the third element
// where index equals 2, corresponds to sum = 2, and so on.
// make sure they''re initialized to 0, which C# does by default
int[] sum = new int[13];

// for 36,000 times, roll two dice, calculate the sum,
// and increment the index corresponding to that sum.
int currentResult = 0;
for (int numberOfRolls = 1; numberOfRolls <= 36000; ++numberOfRolls)
{
currentResult = RollTwoDice();
++sum[currentResult];
}

// print how many times each sum comes up and what percent of the time
// to a 3 decimal precision that sum comes up
for (int sumIndex = 2; sumIndex <= 12; ++sumIndex)
{
Console.WriteLine("{0,5} {1,5} {2:F3}", sumIndex, sum[sumIndex],
(double)sum[sumIndex] / 36000 * 100);
}
}

// return the sum of two randomly generated integers from 1 to 6
// inclusive
static int RollTwoDice()
{
Random r = new Random();
return r.Next(1, 7) + r.Next(1, 7);
}

Thanks in advance,
Jose

推荐答案

我改变了我的代码解决了以下问题。

但是我不在乎让程序像我想要的那样工作

知道我做了什么在我的第一个例子中错了。任何人都可以帮我解释一下

我在之前的帖子中的代码中做错了吗?


谢谢!


工作代码:


静态无效SimulateDiceRolls()

{

//当你掷两个骰子时,有11个可能的总和:

// 2,3,...,10,11,12。定义一个包含13个元素的数组

//其中前两个元素未使用,但是第三个元素

//其中index等于2,对应sum = 2,依此类推。

//确保它们被初始化为0,C#默认情况下确实

int [] sum = new int [13];

随机r = new Random();


//为36,000次,滚动两个骰子,计算总和,

//并增加与该总和相对应的索引。

int currentResult = 0;

for(int numberOfRolls = 1; numberOfRolls< = 36000; ++ numberOfRolls)

{

currentResult + = r.Next(1,7);

currentResult + = r.Next(1,7);

++总和[currentResult];

currentResult = 0;

}


//打印多少次每个总和出现了多少时间

//到2的小数精度总和出现

for(int sumIndex = 2; sumIndex< = 12; ++ sumIndex)

{

Console.WriteLine(" {0,5} {1,5} {2:F3}",sumIndex,sum [sumIndex ],

(双)和[sumIndex] / 36000 * 100);

}

}
I changed my code to the folllowing which fixes the problem.
But I don''t care about getting the program to work as much as I would like
to know what I did wrong in my first example. Can anyone help explain what
I did wrong in the code from my earlier post?

Thanks!

Working code:

static void SimulateDiceRolls()
{
// When you roll two dice, there are 11 possible sums:
// 2, 3, ..., 10, 11, 12. Define an array of 13 elements
// where the first two elements are unused, but the third element
// where index equals 2, corresponds to sum = 2, and so on.
// make sure they''re initialized to 0, which C# does by default
int[] sum = new int[13];
Random r = new Random();

// for 36,000 times, roll two dice, calculate the sum,
// and increment the index corresponding to that sum.
int currentResult = 0;
for (int numberOfRolls = 1; numberOfRolls <= 36000; ++numberOfRolls)
{
currentResult += r.Next(1, 7);
currentResult += r.Next(1, 7);
++sum[currentResult];
currentResult = 0;
}

// print how many times each sum comes up and what percent of the time
// to a 2 decimal precision that sum comes up
for (int sumIndex = 2; sumIndex <= 12; ++sumIndex)
{
Console.WriteLine("{0,5} {1,5} {2:F3}", sumIndex, sum[sumIndex],
(double)sum[sumIndex] / 36000 * 100);
}
}


我认为问题在于你使用Random;对于紧密循环,你需要

来使用单个实例,否则在类似的

时间间隔(相隔很小的时间)内的连续对象将具有相同的种子。 br />

这个shoud修复它:


静态只读随机_rand = new Random();

static int RollTwoDice( )

{

返回_rand.Next(1,7)+ _rand.Next(1,7);

}


Marc

I believe the problem is your usage of Random; for tight-loops you need
to use a single instance, otherwise successive objects in a similar
time interval (tiny amounts of time apart) will have the same seed.

This shoud fix it:

static readonly Random _rand = new Random();
static int RollTwoDice()
{
return _rand.Next(1,7) + _rand.Next(1,7);
}

Marc


我明白了。所以我的超短循环会调用RollTwoDice(),增加一个

计数器,然后,比如说,在下一毫秒之前,它会调用

RollTwoDice(),也许很多在使用新种子之前的时间。所以

因为11个骰子中通常有两三个骰子从未卷入过
36,000次尝试,我猜一个数字在一个

行,然后是其他数字几千次,依此类推。


我会制作一个原始程序的版本,告诉我是否这是

的情况。


感谢您指出我看起来正确的方向!


-Jose
I see. So my super short loop would call RollTwoDice(), increment a
counter, and then, say, before the next millisecond, it would call
RollTwoDice(), perhaps many times before a new seed would be used. So
given that usually two or three out of 11 dice sums never got rolled in
36,000 tries, I''d guess one number got rolled several thousand times in a
row, then another number several thousand times after that, and so on.

I''ll make a version of my original program that will tell me if this is the
case.

Thanks for pointing me in what really looks like the right direction!

-Jose


这篇关于骰子滚动问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆