Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question
    
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
1  
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
add comment

3 Answers

up vote 14 down vote accepted

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.

share|improve this answer
add comment

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.

share|improve this answer
    
+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
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.