为什么System.Random类不是静态的? [英] Why is the System.Random class not static?
问题描述
当您使用 System.Random
类,则必须它的一个实例。为什么不能静态
?因为如果我想0到9之间的随机数,我可以使用静态的方法的 System.Random.Next(INT,INT)
:
When you use the System.Random
class, you must make an instance of it. Why is it not static
? Because if I want a random number between 0 and 9, I can use the static method, System.Random.Next(int, int)
:
int ourRandomNumber = Random.Next(0,9);
那么,为什么不是类只是静态
?
推荐答案
您将无法使用不同的种子,如果它是静态的 - 随机的实例跟踪的状态。
在默认情况下随机使用当前时间作为种子,但重新使用特定的种子(即新的随机(42)
)允许您精确地重复随机数序列 - 他们将始终为相同的种子相同。这一方面是在一些应用中非常重要的。例如,我的世界。
You wouldn't be able to use different seeds if it were static - the Random instance keeps track of that state.
By default Random uses the current time as seed, but re-using a particular seed (i.e. new Random(42)
) allows you to exactly repeat the sequence of random numbers - they will always be the same for the same seed. This aspect is very important in some applications. For example, Minecraft.
这篇关于为什么System.Random类不是静态的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!