0

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 ?


Community
  • 1
  • 1
Andreas Grech
  • 105,982
  • 98
  • 297
  • 360
  • Can you give some more context? What is Translate_Body? What's the XAML look like for your canvas? – DavidN Feb 15 '09 at 16:36
  • I don't think that's relevant in this case, but still, I added the explanation in my post – Andreas Grech Feb 15 '09 at 16:42
  • The random calls are always going to generate the same number. One scenario I can imagine is that you were using the coordinates of the previous "bot" as the starting point of the next bot and offsetting from there... – DavidN Feb 15 '09 at 16:58

4 Answers4

3

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

Dominic Hopton
  • 7,262
  • 1
  • 22
  • 29
  • 1
    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 Feb 16 '09 at 15:32
1

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
  • 2,238
  • 14
  • 18
1

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.

Andreas Grech
  • 105,982
  • 98
  • 297
  • 360
  • 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 Feb 15 '09 at 17:08
  • 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 Feb 16 '09 at 15:44
0

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.
  • 113,939
  • 20
  • 158
  • 187