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 *= num;*

for (int i = 0; i < x.Length; i++)
int num = Random.Range(150, 1590);
if (num > 720)
num = 720 - num;
x = 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!

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;
    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.