从随机数生成器获取相同的数字,即使它不在循环中。为什么? [关闭]

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从随机数生成器获取相同的数字,即使它不在循环中。为什么? [关闭]相关的知识,希望对你有一定的参考价值。

这是我为骰子游戏编写的代码,我是新的C#所以请原谅我效率低下:

public class Dice
{
    static public void DiceRoll()
    {
        Random rnd = new Random();
        int roll1 = rnd.Next(1, 7);
        int roll2 = rnd.Next(1, 7);
        int roll3 = rnd.Next(1, 7);
        int roll4 = rnd.Next(1, 7);
        int roll5 = rnd.Next(1, 7);
        int roll6 = rnd.Next(1, 7);
        int roll7 = rnd.Next(1, 7);
        int roll8 = rnd.Next(1, 7);
        int roll9 = rnd.Next(1, 7);
        int roll10 = rnd.Next(1, 7);
        int roll11 = rnd.Next(1, 7);
        int roll12 = rnd.Next(1, 7);
        int Player1Total = roll1 + roll2 + roll3;
        int Player2Total = roll4 + roll5 + roll6;
        int Player1Total2roll = roll7 + roll8;
        int Player2Total2roll = roll9 + roll10;
        int P1froll = roll11;
        int P2froll = roll12;
        int overallP1 = Player1Total + Player1Total2roll + P1froll;
        int overallP2 = Player2Total + Player2Total2roll + P2froll;
        int Player1Score = 0;
        int Player2Score = 0;
        bool gamerunning = false;
        while (gamerunning == false)
        {
            Console.WriteLine("Press anything to roll your die");
            Console.ReadKey();
            Console.Clear();
            Console.WriteLine("
Player1 first dice is ", roll1);
            Console.WriteLine("Player1 second dice is " + roll2);
            Console.WriteLine("Player1 third dice is " + roll3);
            Console.WriteLine("Your total is " + Player1Total);
            Console.WriteLine("
Your opponents first dice roll is " + roll4);
            Console.WriteLine("Your opponents second dice roll is " + roll5);
            Console.WriteLine("Your opponents third dice roll is " + roll6);
            Console.WriteLine("Your opponents total is " + Player2Total);
            Console.WriteLine("
Player1 first dice is " + roll7);
            Console.WriteLine("Player1 second dice is " + roll8);
            Console.WriteLine("Your total is " + Player1Total2roll);
            Console.WriteLine("
Your opponents first dice roll is " + roll9);
            Console.WriteLine("Your opponents second dice roll is " + roll10);
            Console.WriteLine("Your total is " + Player2Total2roll);
            Console.WriteLine("
Player1 final roll is " + roll11);
            Console.WriteLine("Your total is " + P1froll);
            Console.WriteLine("
Your opponents final roll is " + roll12);
            Console.WriteLine("Your total is " + P2froll);
            if (overallP1 > overallP2)
            {
                Player1Score++;
                Console.WriteLine("
Player 1 Wins");
                Console.WriteLine("The Current Score is:");
                Console.WriteLine("Player 1: " + Player1Score);
                Console.WriteLine("Player 2: " + Player2Score);
            }
            else if (overallP2 > overallP1)
            {
                Player2Score++;
                Console.WriteLine("
Player 2 Wins");
                Console.WriteLine("The Current Score is:");
                Console.WriteLine("Player 1: " + Player1Score);
                Console.WriteLine("Player 2: " + Player2Score);
            }
            if (overallP1 == overallP2)
            {
                Console.WriteLine("
It's a draw");
            }
            if (Player1Score >= 5)
            {
                gamerunning = true;
            }
            else if (Player2Score >= 5)
            {
                gamerunning = true;
            }
            //if ( Player1Score == Player2Score)
            //{
            //    DiceRoll();
            //}
        }
    }
}

问题是我总是从生成器获得相同的数字,即使循环超出了我创建随机数的位置。有谁知道这个问题,并有任何提示。提前致谢。

答案

只需将变量声明移动到循环中,您的代码就像您希望的那样工作。你想在每场比赛中获得一个新的随机数 - 每场比赛都是你的while (gamerunning == false)循环的一次迭代。

private static void Main(string[] args)
    {
        Random rnd = new Random();
        bool gamerunning = false;
        while (gamerunning == false)
        {
            int roll1 = rnd.Next(1, 7);
            int roll2 = rnd.Next(1, 7);
            int roll3 = rnd.Next(1, 7);
            int roll4 = rnd.Next(1, 7);
            int roll5 = rnd.Next(1, 7);
            int roll6 = rnd.Next(1, 7);
            int roll7 = rnd.Next(1, 7);
            int roll8 = rnd.Next(1, 7);
            int roll9 = rnd.Next(1, 7);
            int roll10 = rnd.Next(1, 7);
            int roll11 = rnd.Next(1, 7);
            int roll12 = rnd.Next(1, 7);
            int Player1Total = roll1 + roll2 + roll3;
            int Player2Total = roll4 + roll5 + roll6;
            int Player1Total2roll = roll7 + roll8;
            int Player2Total2roll = roll9 + roll10;
            int P1froll = roll11;
            int P2froll = roll12;
            int overallP1 = Player1Total + Player1Total2roll + P1froll;
            int overallP2 = Player2Total + Player2Total2roll + P2froll;
            int Player1Score = 0;
            int Player2Score = 0;

            Console.WriteLine("Press anything to roll your die");
            Console.ReadKey();
            Console.Clear();
            Console.WriteLine("
Player1 first dice is ", roll1);
            Console.WriteLine("Player1 second dice is " + roll2);
            Console.WriteLine("Player1 third dice is " + roll3);
            Console.WriteLine("Your total is " + Player1Total);
            Console.WriteLine("
Your opponents first dice roll is " + roll4);
            Console.WriteLine("Your opponents second dice roll is " + roll5);
            Console.WriteLine("Your opponents third dice roll is " + roll6);
            Console.WriteLine("Your opponents total is " + Player2Total);
            Console.WriteLine("
Player1 first dice is " + roll7);
            Console.WriteLine("Player1 second dice is " + roll8);
            Console.WriteLine("Your total is " + Player1Total2roll);
            Console.WriteLine("
Your opponents first dice roll is " + roll9);
            Console.WriteLine("Your opponents second dice roll is " + roll10);
            Console.WriteLine("Your total is " + Player2Total2roll);
            Console.WriteLine("
Player1 final roll is " + roll11);
            Console.WriteLine("Your total is " + P1froll);
            Console.WriteLine("
Your opponents final roll is " + roll12);
            Console.WriteLine("Your total is " + P2froll);
            if (overallP1 > overallP2)
            {
                Player1Score++;
                Console.WriteLine("
Player 1 Wins");
                Console.WriteLine("The Current Score is:");
                Console.WriteLine("Player 1: " + Player1Score);
                Console.WriteLine("Player 2: " + Player2Score);
            }
            else if (overallP2 > overallP1)
            {
                Player2Score++;
                Console.WriteLine("
Player 2 Wins");
                Console.WriteLine("The Current Score is:");
                Console.WriteLine("Player 1: " + Player1Score);
                Console.WriteLine("Player 2: " + Player2Score);
            }
            if (overallP1 == overallP2)
                Console.WriteLine("
It's a draw");
            if (Player1Score >= 5)
                gamerunning = true;
            else if (Player2Score >= 5)
                gamerunning = true;
            //if ( Player1Score == Player2Score)
            //{
            //    DiceRoll();
            //}
        }
    }

以上是关于从随机数生成器获取相同的数字,即使它不在循环中。为什么? [关闭]的主要内容,如果未能解决你的问题,请参考以下文章

while循环中的随机数[C编程] [重复]

摆脱程序中的break语句

java 从0-16这17个数字中随机取出100个数,要求其中数字2出现4次,数字5出现7次,数字7出现5次,其他任意

从 0 到 20 的随机数生成器,用户输入一个可以为真或假的数字

皮斯帕克。生成随机数的变压器总是生成相同的数字

如何在javascript中获取纯粹唯一的数字[重复]