WPF: Rendering a canvas at Random Points

2019-08-16 06:21发布

With reference to this programming game I am currently building.

When the game is started, I am generating these robots at supposingly random points on a Canvas, and at first look (adding one or two bots at the same time), this seemed to be working as it should.

...but, when I added a ton of bots at the same time, this is how they where "randomly" appeared on the canvas:

alt text http://img22.imageshack.us/img22/6895/randombotpositionlf7.jpg

The supposedly random points don't seem that random after all...they're just points of a straight line!


Here is how I am calculating the Points:

SetStartingPoint(GetRandomPoint(ArenaWidth, ArenaHeight)); //the width and height are 550, 480 at the moment 

//which calls:

private Point GetRandomPoint(double maxWidth, double maxHeight)
{
        return new Point(new Random().Next(0, (int)(maxWidth-80)), new Random().Next(0, (int)maxHeight));
}

//and ultimately:

private void SetStartingPoint(Point p)
{
        Translate_Body.X = (double)p.X;
        Translate_Body.Y = (double)p.Y;
}

As regards the above code, Translate_Body is of type TranslateTransform of the robot (canvas), so by assigning its X and Y properties, it will change its position to the new values


What am I missing here?


[UPDATE] Solution:

The problem was like you all suggested, the numbers weren't being seeded properly because of the new instantiations.

I now changed the code to use a single Random variable and to seed all the points from it.

But I still can't understand why the points where being generated at a seemingly straight line of coordinates. Is anyone able to explain this ?


标签: wpf math random
4条回答
Root(大扎)
2楼-- · 2019-08-16 06:49

Can't see anything strange from here aside from that you should use one instance of Random instead of creating a new seed all the time. If your system is fast enough, you might be getting the same seed both times.

If this doesn't fix it, check your setters and anything affected by setting/drawing Translate_Body.X and Translate_Body.Y once more. I'm guessing when you draw it, you're drawing at (X,X) or (Y,Y) instead of (X,Y)...

查看更多
你好瞎i
3楼-- · 2019-08-16 06:50

The problem is that you keep making new Random() objects, and then take the very first random number. But the Random() object is initialized with the time, so it always get the same value.

Instead do this:

private System.Random R = new System.Random();
private Point GetRandomPoint(double maxWidth, double maxHeight)
{
    return new Point(R.Next(0, (int)(maxWidth - 80)), R.Next(0, (int)maxHeight));
}

This will make a single Random() object and then call it repeatedly. This will also perform better because you aren't initializing quite so many objects.

查看更多
孤傲高冷的网名
4楼-- · 2019-08-16 07:01

I believe the problem is that your random numbers aren't being seeded properly. Use a single instance of Random, rather than multiple ones, and you should see much improvement.

User added "why they were in a straight line" -- probably because of the default progression of the generated random numbers based on the seed. wikipedia would be a good place to start -- I'm no math expert, so won't try to explain the core of the world of random numbers. :)

查看更多
一纸荒年 Trace。
5楼-- · 2019-08-16 07:04

By default, the Random class uses the current time as the seed. Since the time only advances every few milliseconds, most of your "random" numbers will actually be exactly the same. Construct your Random object just once, and subsequent calls to Next() should give you more random results.

查看更多
登录 后发表回答