我如何在0-1随机数生成器C#中保持最高条纹

时间:2018-10-14 11:47:15

标签: c#

我想测试一下连胜赔率,但我碰到了一些泡菜,请事先为我(很可能是)丑陋的代码辩解。

所以我正在创建一个随机数0-1,其中1 =赢,0 =输。 我想保持最高的连胜纪录。因此,当它丢失时,它变为整数,如果获胜,则丢失连胜变为第二个整数,而第一个整数再次变为0,我开始我的连胜,发生相同的情况,但由于当前代码的某些原因,它没有做我想做的。 如果我这样做,则表示我的连胜纪录比赢得的总数少2倍。谢谢您提前提供的帮助,如果我的解释难以理解(英语不是我的母语),我感到抱歉。

        Random rng1 = new Random();
        bool done = false;
        int win;
        int lose;
        win = 0;
        lose = 0;
        int lose1;
        int lose2;
        int win1;
        int win2;
        win1 = 0;
        win2 = 0;
        lose1 = 0;
        lose2 = 0;
        int roll1 = 0;
        while(done == false)
        {

            int roll = rng1.Next(0, 2);
            Console.WriteLine(roll);
            if(roll == 1)
            {
                win++;
                win1 = win;
                if(lose1 < lose2)
                {
                    lose2 = lose1;
                    lose1 = 0;

                }



            }
            else
            {

                lose++;
                lose1 = lose;

                if(win1 >= win2)
                {

                    win2 = win1;
                    win1 = 0;

                }

            }

            roll1++;
            if(roll1 == 100)
            {

                done = true;

            }

        }

        Console.WriteLine("lose streak" + lose2 + " win streak" + win2 + " lose" + lose + " win" + win);
        Console.ReadKey();



    }
}

}

2 个答案:

答案 0 :(得分:0)

我修复了您的代码,同时使其与您的代码尽可能接近。具体来说,我只更改了2行。老实说,我只是有点无聊,但是无论哪种方式,您的问题与该网站实际上并没有任何关系,您只是存在逻辑缺陷。下次尝试自己考虑一下,这是学习的唯一方法。

Random rng1 = new Random();
bool done = false;
int win;
int lose;
win = 0;
lose = 0;
int lose1;
int lose2;
int win1;
int win2;
win1 = 0;
win2 = 0;
lose1 = 0;
lose2 = 0;
int roll1 = 0;

while (done == false)
{
    int roll = rng1.Next(0, 2);
    Console.WriteLine(roll);
    if (roll == 1)
    {
        win++;
        win1++; // A change
        if (lose1 < lose2)
        {
            lose2 = lose1;
            lose1 = 0;
        }
    }
    else
    {
        lose++;
        lose1 = lose;

        if (win1 >= win2)
        {
            win2 = win1;
        }
        win1 = 0; // A change
    }

    roll1++;
    if (roll1 == 100)
    {
        done = true;
    }
}

Console.WriteLine("lose streak" + lose2 + " win streak" + win2 + " lose" + lose + " win" + win);
Console.ReadKey();

答案 1 :(得分:0)

这可以解决问题。我创建了一些小的数组来跟踪所有数字。您可以通过使用例如6个数字(骰子)轻松扩展示例。仅更改“可能性”

using System;

namespace Gamble
{
    public class Dice
    {
        class Program
        {
            static void Main(string[] args)
            {
                const int possiblities = 2;

                Random rng1 = new Random();
                int[] side = new int[possiblities];
                int[] maxSequences = new int[possiblities];

                int lastRoll = 0;
                int sequenceCount = 0;
                for (int rollCount = 0; rollCount < 100; rollCount++)
                {
                    int roll = rng1.Next(0, possiblities);
                    Console.WriteLine(roll);
                    if (lastRoll != roll)
                    {
                        if (maxSequences[lastRoll] < sequenceCount) maxSequences[lastRoll] = sequenceCount;
                        sequenceCount = 0;
                        lastRoll = roll;
                    }
                    sequenceCount++;

                    side[roll]++;

                }

                Console.WriteLine("lose streak:" + maxSequences[0] + " win streak:" + maxSequences[1] + " lose:" + side[0] + " win:" + side[1]);
                Console.ReadKey();

             }
        }
    }

}