tags:

views:

376

answers:

4
+3  A: 

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. :)

dhopton
They are in a straight line because the X and Y values are always the same. Why would they be the same? Because each one is generated by a NEW Random() object. since they are created at the exact same time, they naturally have the exact same value.
SunriseProgrammer
+1  A: 

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.

Jim Arnold
A: 

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)...

lc
+1  A: 

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.

SunriseProgrammer
Actually, I placed the R random variable in the method itself, as to avoid cluttering the global space. (...yea i know, I am heavily influenced by JavaScript)
Andreas Grech
Umm...does that really work? what't the syntax for declaring, in C#, a static variable inside a method?I ask because a quick web search shows lots of comments from people wishing that C# had this ability.(And as an ex-C++ programmer, I wish I had the ability, too)
SunriseProgrammer