抛硬币问题

发布于 2024-11-03 07:52:28 字数 1370 浏览 4 评论 0原文

我一直在玩并写了这段小代码。我试图将一枚硬币抛掷规定的次数,然后计算得到的反面和正面的数量。所以这就是:

private void Start_Click(object sender, EventArgs e)
{
    int headss = 0;
    int tailss = 0;
    int random2, g;
    string i = textBox1.Text;
    int input2, input;
    bool NumberCheck = int.TryParse(i, out input2);

    if (textBox1.Text == String.Empty) // check for empty string, when true
        MessageBox.Show("Enter a valid number between 0 and 100000.");
    else // check for empty string, when false
        if (!NumberCheck) // number check, when false
        {
            textBox1.Text = String.Empty;
            MessageBox.Show("Enter a valid number between 0 and 100000.");
        }
        else
        {
            input = Convert.ToInt32(textBox1.Text);

            for (g = 0; g < input; g++)
            {
                Random random = new Random();
                random2 = random.Next(2);

                if (random2 == 0)
                {
                    headss++;
                }
                else if (random2 == 1)
                {
                    tailss++;
                }
            }
        }

    heads.Text = Convert.ToString(headss);
    tails.Text = Convert.ToString(tailss);
}

问题是我在显示内容时不断遇到问题。它甚至还没有接近显示他们正确的结果。有什么想法吗?

编辑。解决方案:将以下第 3 行向上移动 :D

Random random = new Random();

I have been playing around and wrote this little piece of code. I am trying to flip a coin defined number of times and then count how many tails and heads I am getting. So here it is:

private void Start_Click(object sender, EventArgs e)
{
    int headss = 0;
    int tailss = 0;
    int random2, g;
    string i = textBox1.Text;
    int input2, input;
    bool NumberCheck = int.TryParse(i, out input2);

    if (textBox1.Text == String.Empty) // check for empty string, when true
        MessageBox.Show("Enter a valid number between 0 and 100000.");
    else // check for empty string, when false
        if (!NumberCheck) // number check, when false
        {
            textBox1.Text = String.Empty;
            MessageBox.Show("Enter a valid number between 0 and 100000.");
        }
        else
        {
            input = Convert.ToInt32(textBox1.Text);

            for (g = 0; g < input; g++)
            {
                Random random = new Random();
                random2 = random.Next(2);

                if (random2 == 0)
                {
                    headss++;
                }
                else if (random2 == 1)
                {
                    tailss++;
                }
            }
        }

    heads.Text = Convert.ToString(headss);
    tails.Text = Convert.ToString(tailss);
}

The problem is that I keep getting problems while displaying the content. It's not even close to display they right result. Any ideas?

EDIT. Solution: move following line 3 lines up :D

Random random = new Random();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

对岸观火 2024-11-10 07:52:28

而不是

for (g = 0; g < input; g++)
{
   Random random = new Random();
   random2 = random.Next(2);
}

声明单个 Random 供整个使用:

private Random randomGenerator = new Random();
private void Start_Click(object sender, EventArgs e)
{
    // ...
    for (g = 0; g < input; g++)
    {
        random2 = randomGenerator.Next(2);
    }
    // ...
}

Instead of

for (g = 0; g < input; g++)
{
   Random random = new Random();
   random2 = random.Next(2);
}

Declare a single Random for use throughout:

private Random randomGenerator = new Random();
private void Start_Click(object sender, EventArgs e)
{
    // ...
    for (g = 0; g < input; g++)
    {
        random2 = randomGenerator.Next(2);
    }
    // ...
}
咿呀咿呀哟 2024-11-10 07:52:28

您应该仅使用一个 Random 对象来生成良好的(与默认 Random 一样好的)随机序列。

You should use only one Random object to generate good (as good as default Random does) random sequence.

篱下浅笙歌 2024-11-10 07:52:28

random 的默认构造函数将系统时间作为种子。因此,如果您在短时间内生成大量它们,它们都会生成相同的随机数序列。将随机对象拉出循环就不会出现这种效果。

The default constructor for random take the systmem time as seed. Therefore, if you generate lots of them in a short amount of time they will all generate the same sequence of random numbers. Pull the random object out of the loop and this effect will not occur.

叹梦 2024-11-10 07:52:28

使用随机生成器。该代码将计算硬币被翻转的次数。将以连续 3 个 HEADS 结束。

private RandomGenerator rgen = new RandomGenerator ();

public void run () {

    int value = 0;
    int total = 0;
    while (value != 3) {
        String coinFlip =  rgen.nextBoolean() ? "HEADS" : "TAILS";
        println (coinFlip);
        if (coinFlip == "HEADS") {
           value+=1;
        } else {
           value=0;
        }
        total +=1;
    }
    println ("It took "+total+" flips to get 3 consecutive heads");     
}

With RandomGenerator. This code will count how many times coin has been flipped. It will end with 3 consecutive HEADS.

private RandomGenerator rgen = new RandomGenerator ();

public void run () {

    int value = 0;
    int total = 0;
    while (value != 3) {
        String coinFlip =  rgen.nextBoolean() ? "HEADS" : "TAILS";
        println (coinFlip);
        if (coinFlip == "HEADS") {
           value+=1;
        } else {
           value=0;
        }
        total +=1;
    }
    println ("It took "+total+" flips to get 3 consecutive heads");     
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文