# Generate Random Numbers a Distance Apart From Each Other

So I have a galaxy with randomly generated solar systems spread throughout. I do this by making a random array for x values and a random array for z values and instantiating the solar system object and setting each object at random coordinates. However, sometimes 2 or 3 solar systems get too close and could even end up on top of each other. Any idea on how to make it so I can generate a random number but also have each number be at least like 10 apart from each other?

```
public int[] x;
public int[] z;
public void new(){
x = new int[50];
z = new int[50];
for (int i = 0; i < z.Length; i++)
{
int num = Random.Range(150, 1590);
if (num > 720)
num = 720 - num;
z[i] = num;
}
for (int i = 0; i < x.Length; i++)
{
int num = Random.Range(150, 1590);
if (num > 720)
num = 720 - num;
x[i] = num;
}
}
```

There's some code if anyone needs it. The reason the random range min is 150 is to account for the galactic bulge and if the number is greater than 720 it makes it a negative number so that I can generate numbers from -720 to 720.

Thanks for any help!

**Answer** by Bunny83
·
Jun 27, 2016 at 11:16 AM

I don't quite get the point of that strange number generation. Your actual range that you generate is

-869 to 0 and 150 to 720. So you have a gap between 0 and 150 and your galaxy isn't centered around 0,0

If you wanted to concentrate the values around the center you would have to do something like:

```
int num = Random.Range(-870, 870);
if (num > 0)
num -= 150;
else
num += 150;
```

This will give you a range of -720 to 720 with double the desity between -150 and 150. Keep in mind that you will generate a rectange here and not a circle. So the high density area in the center will also be a rectangle. You might want to generate the position based on a random angle around the center and a random distance.

```
float angle = Random.Range(0,360);
float dist = Random.Range(0f,870f);
if (dist > 150)
dist -= 150;
Vector3 p = Quaternion.AngleAxis(angle, Vector3.up) * Vector3.forward * dist;
```

This will generate points in a circular fashion and the "bulge" around the center is also circular.

About your actual problem there are several ways to solve this problem.

You could simply generate more points than you need and when done you simply iterate over all points and check the distance between the current and every other point. If a distance is too close you would simply remove one of the points. This has a constant complexity of O(n²).

The other way would be todo the same as in the first approach, but if two points are too close you simply move them apart, each half the overlapping distance. The problem here is that after one iteration through all points the result is not guaranteed to be correct as the spreading of two points can cause new problems with other points. In the high density area this could cause a lot of problems and you would need many iterations until all points have a valid position. If there are way too many points in a certain area it could take thousands of iterations where each iteration has a complexity of O(n²).

The first approach is the easiest one to implement however the resulting amount of points can not be determined before the process. If you need a certain amount you could generate 2 or 3 times the points you need, then delete all point that overlap. If the resulting amout is larger than the wanted amount you can simply randomly delete points from the array until the amount is correct. However if there are less points than the wanted amount you would need to generate more points and restart the process.

The second one is the most natural approach and more like a physics engine solving problem. As long as there are overlaps you apply correction forces / movement until there are no overlaps. This approach has a set amount of points which does not change but it's runtime can explode if the values are choosed too large. Also this approach can cause to "push" your points outside your initial radius.

Or there is one more approach which is a little different than the 1st approach. When you generate a new number check it for closeness with other generated numbers up to now. If it is close then ignore it and generate a new number. But again this can take enormous number of iterations if system keeps generating number which is closest to other points.

Thank you so much for a long and detailed answer, especially for pointing out the flaw in the random generation!

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Randomly generate blocks on a flat map 0 Answers

Random Scenes Without Repetition 1 Answer

Can no1 help me??? 1 Answer

How to generate a random color? 5 Answers