Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by nichcar98 · Jun 27, 2016 at 10:19 AM · randomrandom.rangegeneration

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?

http://imgur.com/Ysnh2YR

     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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image HarshadK · Jun 27, 2016 at 11:22 AM 1
Share

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.

avatar image nichcar98 · Jun 27, 2016 at 06:54 PM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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

Need help with specifics in Random.Range 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges