Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by troponeme · Nov 13, 2013 at 01:06 AM · randomswitchprocedural-generationinterval

Random int generation between two intervals

I'm making a game with procedural generation for each level. At the beginning I want to create a random number of game objects(done) at a random distance(sorta done). So right now I have a random number of game objects being formed at a distance (with Random.Range(1000, 6000)). But I don't want it to be just positive, for example, generate a number between -6000 and 6000 excluding anything smaller than 1000 and greater than -1000. After looking around the only way that came to me was using a random number between 1 and 4 where the number would be the quadrant it'd be in and then use a switch loop for each possible case.

             switch (wormhole[curWormholeGen]){
             case 1:
                 switch(genQuadrant){
                 case 1:
                     Instantiate (wormholeType1, transform.position + new Vector3(Random.Range(minX, maxX), 0, Random.Range (minZ,maxZ)), transform.rotation);
                     break;
                 case 2:
                     Instantiate (wormholeType1, transform.position + new Vector3(-Random.Range(minX, maxX), 0, Random.Range (minZ,maxZ)), transform.rotation);
                     break;
                 case 3:
                     Instantiate (wormholeType1, transform.position + new Vector3(-Random.Range(minX, maxX), 0, -Random.Range (minZ,maxZ)), transform.rotation);
                     break;
                 case 4:
                     Instantiate (wormholeType1, transform.position + new Vector3(Random.Range(minX, maxX), 0, -Random.Range (minZ,maxZ)), transform.rotation);
                     break;
                 }


this is just a snippet from a part of the code. There's also a random length for the array etc... There must be a more effective way than this right?

Thanks for the help,

Troponeme

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
0
Best Answer

Answer by rutter · Nov 13, 2013 at 01:09 AM

The bad news is that there's no built-in function for that. The good news is that it's very easy to write your own.

Something like this will do the trick for a single integer:

     //number will have absolute value between min, max
     //number has 50% chance of being pos/neg
     public static int AbsRange(int min, int max) {
             int i = Random.Range(min, max);
             return Random.value < 0.5f ? i : -i;
     }

If you wanted, you could write similar functions for floats, vectors, or whatever other types you might need.

Comment
Add comment · Show 1 · 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 troponeme · Nov 16, 2013 at 12:03 PM 0
Share

Thanks, ins$$anonymous$$d of changing it to create vectors I changed it for floats and created a new vector with a generated number each time. Probably would have saved me time overall to just go with vectors ins$$anonymous$$d of floats but oh well! It works!

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

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

instantiation at interval times 3 Answers

Making enemies jump at random (c#) 2 Answers

Request: Advice on delving into "Procedurally Generated Content(PGC)". C# 2D Unity 2 Answers

Procedural Generation 2 Answers

How do i spawn a square in random position within an area but only at certain x and y intervals? 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