11
\$\begingroup\$

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?

\$\endgroup\$
4
  • \$\begingroup\$ 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? \$\endgroup\$ Commented Jan 11, 2011 at 5:12
  • \$\begingroup\$ 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 \$\endgroup\$
    – Robb
    Commented Jan 11, 2011 at 5:21
  • 1
    \$\begingroup\$ The Random class your talking about is part of the .NET framework, not XNA. Just fyi. \$\endgroup\$ Commented Jan 11, 2011 at 18:11
  • \$\begingroup\$ @Omnion - I'm aware. I was a bit tired when creating the question ;) \$\endgroup\$
    – Robb
    Commented Jan 11, 2011 at 19:17

3 Answers 3

16
\$\begingroup\$

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.

\$\endgroup\$
7
\$\begingroup\$

Shawn Hargreaves to the rescue!

http://blogs.msdn.com/b/shawnhar/archive/2009/12/17/the-psychology-of-randomness.aspx http://blogs.msdn.com/b/shawnhar/archive/2010/09/10/links-5-charles-bloom-on-randomness.aspx

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.

\$\endgroup\$
1
  • \$\begingroup\$ +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) \$\endgroup\$
    – Spooks
    Commented Jan 17, 2011 at 20:22
0
\$\begingroup\$

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.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .