简体   繁体   English

随机对象未在C#中处理

[英]Random object not disposing in C#

I am working on a smooth terrain generation algorithm in C# and using XNA to display the data. 我正在使用C#中的平滑地形生成算法并使用XNA来显示数据。

I am making it so it creates a new point halfway between each point per iteration, at a random height between the two. 我正在制作它,因此它在每次迭代的每个点之间创建一个新点,在两者之间的随机高度。 This works OK, and I have set it so that on the second iteration it chooses a random point like in slide two, rather than trying to make a new point between points that are on the same axis. 这工作正常,我设置它,以便在第二次迭代时它选择一个像幻灯片2中的随机点,而不是尝试在同一轴上的点之间创建一个新点。

What is happening is that the loop is using the same random value from the previous iteration: http://i.stack.imgur.com/UmWr7.png 发生的事情是循环使用上一次迭代中的相同随机值: http//i.stack.imgur.com/UmWr7.png

This is not ideal obviously, as it is not a proper random generation. 这显然不是理想的,因为它不是一个适当的随机生成。

If I use a Thread.Sleep(20) after each point generation it works correctly: http://i.stack.imgur.com/KziOg.png 如果我在每个点生成后使用Thread.Sleep(20)它可以正常工作: http//i.stack.imgur.com/KziOg.png

I don't want to have to use the Sleep workaround if possible as it is very slow, and I would like to use this in real-time. 我不想在可能的情况下使用Sleep解决方法,因为它非常慢,我想实时使用它。 I'm pretty sure this has something to do with the C# garbage collector. 我很确定这与C#垃圾收集器有关。

Here is my Get Point Code 这是我的获取点代码

Random r = new Random();
int x = (p1.X + p2.X) / 2;
int y;
if (!initial)
       y = r.Next(Math.Min(p1.Y, p2.Y), Math.Max(p1.Y, p2.Y));
else
       y = r.Next(Math.Min(p1.Y, p2.Y) - Game1.screenHeight / 2, Math.Max(p1.Y, p2.Y) + Game1.screenHeight / 2);
return new Point(x, y);

Is the garbage collection a part of the issue? 垃圾收集是问题的一部分吗?

Any suggestions or solutions on solving this?? 解决这个问题的任何建议或解决方案

Probably you are creating a new Random object in a loop. 可能你是在循环中创建一个新的Random对象。

for (/* ... */) {
    int x = new Random().Next(); // Don't do this!
    // ...
}

Try to create only one instance of Random at program startup, then re-use it. 尝试在程序启动时只创建一个Random实例,然后重新使用它。 If you have multiple threads, then you could use one random object per thread. 如果你有多个线程,那么你可以为每个线程使用一个随机对象。

It sounds like you're probably creating a new instance of Random on each iteration 1 . 听起来你可能在每次迭代1上创建一个新的Random实例。 That will take its seed from the current time - so without a sleep, you end up with the same value repeatedly; 这将从当前时间开始播种 - 所以没有睡眠,你会反复得到相同的值; with a sleep you end up getting a different seed. 睡着了,你最终会得到一个不同的种子。

The answer is to reuse one Random instance throughout your program - but taking note of the fact that Random isn't thread-safe. 答案是在整个程序中重用一个 Random实例 - 但要注意Random不是线程安全的事实。 If all your work is being done in the UI thread, you'll be fine - but otherwise you may want to use one of the approaches I've given in my article about Random (which talks about this problem more). 如果你的所有工作都在UI线程中完成,你会没事的 - 但是你可能想要使用我在关于Random文章中给出的一种方法(更多地讨论这个问题)。


1 And yes, now that you've posted the code that is indeed the case. 1是的,既然您已经发布了确实如此的代码。

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

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