# Random enemy placement on a 2d grid

I want to place my items and enemies randomly (or as randomly as possible). At the moment I use XNA's Random class to generate a number between 800 for X and 600 for Y. It feels like enemies spawn more towards the top of the map than in the middle or bottom. I do not seed the generator, maybe that is something to consider.

Are there other techniques described that can improve random enemy placement on a 2d grid?

• This question seems malformed. You want to place things randomly, without using a source of random data? That is a contradiction. What are you actually trying to achieve? – Andrew Russell Jan 11 '11 at 5:12
• I would like to know if there are any techniques other than relying on XNA's Random class to generate a number between a min and max. I will try to make a more clear question – Robb Jan 11 '11 at 5:21
• The Random class your talking about is part of the .NET framework, not XNA. Just fyi. – Michael Coleman Jan 11 '11 at 18:11
• @Omnion - I'm aware. I was a bit tired when creating the question ;) – Robb Jan 11 '11 at 19:17

Random placement doesn't look random enough to the eye. You'll probably want the enemies spaced out a bit more evenly. Take a look at this article about Poisson disk sampling, or read about blue noise.

Shawn Hargreaves to the rescue!

When you think random ditribution, you really mean even distribution. .Net's Random is thinking random. You need something else to even it out. The Poisson disk sampling method amitp linked to looks perfect for your needs.

• +1 took the words right out of my mouth, even distribution seems to be mix with random distribution all the time (I myself have made the same mistake) – Spooks Jan 17 '11 at 20:22

The following C# code will generate a set of uniform points for you:

Random rand = new Random(); // creates a new random number generator with a time-based seed
int X, Y;
for(int i = 0; i < numberOfPoints; i++)
{
X = rand.Next(800); // generates a random int between 0 and 799, inclusive
Y = rand.Next(600); // generates a random int between 0 and 599, inclusive
AddPointToSet(X,Y); // write this method however you want
}


Note that when you use the Random() constructor with no parameters, the Random object you create is seeded based on the time, so it's going to be essentially random unless you're doing it repeatedly with very little time in between calls.