简体   繁体   English

我的代码只能在“进入”中正常运行,而不能正常运行

[英]My code only works properly in 'step into' and not normally

Basically i want my code to generate two random numbers and then what they equal. 基本上我希望我的代码生成两个随机数,然后等于它们。 It works how it should when i go into step into but not normally and i can't for the life of me figure out why! 当我进入但不是正常情况下,它应该如何运作,而我无法为自己的生活弄清楚原因! when i run it normally the two numbers are always the same, however when i step into the program they are different. 当我正常运行它时,这两个数字总是相同的,但是当我进入程序时,它们是不同的。 I want them both to be random. 我希望他们都是随机的。 I'm using visual studio 2015 if that helps. 如果有帮助,我正在使用Visual Studio 2015。 Thank you in advance! 先感谢您!

class Program
{
    static void Main(string[] args)
    {
        DiceRoll DR = new DiceRoll();
        DR.SecondRolledDice();
    }
}  

public int RolledDice()
    {
        Random numberGenerator;
        numberGenerator = new Random();
        int firstdiceroll = numberGenerator.Next(1, 7);

        return firstdiceroll;
    }        

    public void SecondRolledDice()
    {
        Random SecondnumberGenerator;
        SecondnumberGenerator = new Random();
        int seconddiceroll = SecondnumberGenerator.Next(1, 7);

        DiceRoll DR = new DiceRoll();
        var diceroll = DR.RolledDice();

        string NumberName;
        string NumberNameTwo;
        string sum;
        string[] SecondNumberNames;
        SecondNumberNames = new string[12] { "One", "Two", "Three", "Four", "Five", "Six", "seven", "eight", "Nine", "ten", "eleven", "twelve"};

        NumberName = SecondNumberNames[seconddiceroll - 1];
        NumberNameTwo = SecondNumberNames[diceroll - 1];
        sum = SecondNumberNames[(diceroll + seconddiceroll) - 1];

        Console.WriteLine("{0} Plus {1} Equals {2} ", NumberName, NumberNameTwo, sum);         
    }  

From System.Random documentation: System.Random文档中:

The Random() constructor uses the system clock to provide a seed value. Random()构造函数使用系统时钟来提供种子值。 This is the most common way of instantiating the random number generator. 这是实例化随机数生成器的最常见方法。

If the same seed is used for separate Random objects, they will generate the same series of random numbers. 如果将相同的种子用于单独的随机对象,则它们将生成相同系列的随机数。

Also, from the Avoiding multiple instantiations section: 另外,从避免多重实例化部分:

Initializing two random number generators in a tight loop or in rapid succession creates two random number generators that can produce identical sequences of random numbers. 以紧密循环或快速连续方式初始化两个随机数生成器会创建两个随机数生成器,它们可以产生相同的随机数序列。 In most cases, this is not the developer's intent and can lead to performance issues, because instantiating and initializing a random number generator is a relatively expensive process. 在大多数情况下,这不是开发人员的意图,并且可能导致性能问题,因为实例化和初始化随机数生成器是一个相对昂贵的过程。

Both to improve performance and to avoid inadvertently creating separate random number generators that generate identical numeric sequences, we recommend that you create one Random object to generate many random numbers over time, instead of creating new Random objects to generate one random number. 为了提高性能并避免无意间创建单独的随机数生成器以生成相同的数字序列,我们建议您创建一个随机对象以随时间推移生成许多随机数,而不是创建新的随机对象以生成一个随机数。

You need to instantiate random once, then call Next method for both dice. 您需要实例化随机一次,然后为两个骰子调用Next方法。

Your RolledDice() method is recreating the Random object each time it's called, which is what is causing the same numbers to be generated. 您的RolledDice()方法在每次调用它时都会重新创建Random对象,这就是导致生成相同数字的原因。

Simply declare your Random numberGenerator = new Random(); 只需声明您的Random numberGenerator = new Random(); once as a global variable (maybe, for good practice create it on the main method instantiation) and then reuse it. 一次作为全局变量(也许,为了良好实践,可以在主方法实例化上创建它),然后重用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 写入文件仅在“步入”调试模式下工作? - Writing to file works in “step in” debug mode only? C# 编译我的代码只适用于我的电脑,没有其他人 - C# Compiling my code only works for my PC and no one elses LogonUser仅适用于我的域 - LogonUser works only for my domain 第二次我在HttpPost中使用“ getResponse()”时,它只能逐步调试 - The second time I use “getResponse()” in an HttpPost, it works only debugging step by step 我的emgu cv(c#)代码仅在一个图像上起作用,而在其他图像上崩溃 - My emgu cv (c#) code only works on one image and crash for other image 使用 C# 获取存储过程的参数仅在我在代码中放置断点时才有效 - Getting out parameter of stored procedure with C# only works when I put a breakpoint in my code 只有在我的代码中显式访问属性时,才能通过反射访问任务属性 - Accessing Task properties via reflection only works if the properties are explicitly accessed in my code 为什么我的Release只在我的机器上工作? - Why my Release only works on my machine? TSQL正常运行,但不能通过sp_executesql - TSQL that works normally but not through sp_executesql C#代码仅在步骤中给出预期结果? - C# code only gives expected results on step through?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM