[英]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.