简体   繁体   English

Random()不生成新的随机数

[英]Random() not generating new random numbers

Any help or insight on this would be appreciated. 任何帮助或有识之士将不胜感激。 Still kind of a noob with c#, so please bear with me. 还是有点C#的菜鸟,所以请忍受我。 Here's my problem: 这是我的问题:

List<Vector2> vectorList = new List<Vector2>();

for (int i = 0; i < 5; i++)
{
    Vector2 tempVector = new Vector2(i, i);
    vectorList.Add(tempVector);
}

This works fine, the output I get is 5 vectors with the following values: (0, 0), (1, 1), (2, 2).. and so on... 这工作正常,我得到的输出是5个向量,其值如下:(0,0),(1、1),(2,2)..依此类推...

I need to get random positions, so i did myself a little function called RandomizePosition(). 我需要获得随机位置,所以我为自己做了一个名为RandomizePosition()的小函数。

for (int i = 0; i < 5; i++)
{
    Vector2 tempVector = RandomizePosition();
    vectorList.Add(tempVector);
}

The output i get from this is 5 vectors, containing all the same positions. 我从中得到的输出是5个向量,包含所有相同的位置。 I'm guessing there is a referencing problem, or the function get called only once or something. 我猜有一个引用问题,或者该函数仅被调用一次或某事。 Weird thing is, if I go through each loop in debug mode, the output is what I would expect, 5 vectors with different X and Y values, so i guess the function isn't the problem. 奇怪的是,如果我在调试模式下经历每个循环,那么输出就是我所期望的5个向量,它们具有不同的X和Y值,所以我想函数不是问题。 (or rather it probably is, but not the randomizing logic inside it). (或者可能是,但不是其中的随机逻辑)。

Any ideas on why this is happening, and some insight on how to fix this? 为什么会发生这种情况的任何想法,以及如何解决此问题的一些见解? Can't you call a function from a loop? 您不能从循环中调用函数吗? Or how can i make sure the function gets called on each iteration? 或者如何确保每次迭代都调用该函数? Thanks! 谢谢!

Here's the RandomizePosition() function, if it can help: 如果可以的话,这是RandomizePosition()函数:

private Vector2 RandomizePosition()
{
   Random rand = new Random();

   int seedX = rand.Next(1, 14);
   int seedY = rand.Next(1, 14);

   int posX = 32;
   int posY = 32;

   if (seedX != 1)
       posX += seedX * 64;

   if (seedY != 1)
       posY += seedY * 64;

   return new Vector2(posX, posY);
}

You haven't shown your RandomizePosition() method, but it is likely that you are calling randomise with the same time seed in quick succession, ie creating the Random object inside your RandomizePosition() method. 您没有显示您的RandomizePosition()方法,但是您可能很快连续调用具有相同时间种子的randomise,即在RandomizePosition()方法内创建Random对象。

To stop this happening create the Random object outside your RandomizePosition() method. 为了阻止这种情况的发生,请在RandomizePosition()方法外部创建Random对象。

Update : you have now shown the code for RandomizePosition() and that is indeed the problem. 更新 :现在您已经显示了RandomizePosition()的代码,这确实是问题所在。

As mentioned by @dlev , the reason you see the expected behavior when you step through the code is that you are causing enough time to elapse for your newly created Random() object to use a new seed (since by default it is based on the current time) . @dlev所提到的那样 ,您在遍历代码时看到预期的行为的原因是,您正在花费足够的时间让新创建的Random()对象使用新的种子(因为默认情况下,它基于当前时间) 。

you have declared the rand variable like this Random rand = new Random(); 您已经声明了rand变量,例如: Random rand = new Random(); which sholud not be done make the rand variable a static one and declare it in your class not in your method that time you will not be having such troubles 不能做到这一点,使rand变量成为静态变量,并在您的类中而不是在您的方法中声明它,那时候您就不会有这种麻烦了

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM