Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 掷硬币问题_C#_Coin Flipping - Fatal编程技术网

C# 掷硬币问题

C# 掷硬币问题,c#,coin-flipping,C#,Coin Flipping,我一直在玩,写了这段代码。我试着把一枚硬币掷几次,然后数一数我得到了多少个正面和反面。这就是: 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.TryPars

我一直在玩,写了这段代码。我试着把一枚硬币掷几次,然后数一数我得到了多少个正面和反面。这就是:

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);
}

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

随机对象的默认构造函数将systmem时间作为种子。因此,如果你在短时间内生成了大量的随机数,它们都会生成相同的随机数序列。将随机对象从循环中拉出,此效果将不会发生。

而不是

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

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

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");     
}

显示器看起来不错,但翻转不好
Random
的默认种子基于系统时钟,其分辨率不是很高。通过每次在循环中创建一个新的
Random
对象,您将在多次迭代中反复获得相同的种子,因此您的翻转不会非常随机。谢谢。这是我的工作。它们之间是否有性能差异?这种新方法更好,但可能无法测量。根据经验,您是否认为反复创建一个对象比重用一个对象慢?
private Random randomGenerator = new Random();
private void Start_Click(object sender, EventArgs e)
{
    // ...
    for (g = 0; g < input; g++)
    {
        random2 = randomGenerator.Next(2);
    }
    // ...
}
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");     
}