• 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 Raiio · Oct 16, 2017 at 01:18 PM · randomproblemageneratornumbers

(C#) Problem With Random Number Generator

  private int randomIndex = 0;
     public void GiveIndex()
     {
         float randomValue = Random.value;
         if (randomIndex >= 0 && randomIndex < 5)
         {                                                                                                        // FIRST SET starts here
             if (randomValue <= 0.5) { randomIndex = 0; }
             if (randomValue > 0.85) { randomIndex = Random.Range(1, 5); }
             if (randomValue > 0.5 && randomValue <= 0.85) {randomIndex = 5;}        
         }                                                                                                       // FIRST SET ends here
         
         if (randomIndex >= 5 && randomIndex < 11)
 
         {                                                                                                      // SECOND SET starts here 
             if (randomValue <= 0.5) { randomIndex = 6; }
             if (randomValue > 0.85) { randomIndex = Random.Range(7, 11); }
             if (randomValue > 0.5 && randomValue <= 0.85) {randomIndex = 0;} 
         }                                                                                                     // SECOND SET ends here
    }
  

  • Code explanation:

This is a piece of code that's basically supposed to set randomIndex to a certain number with some probability, There are two sets of numbers (from 0 to 5 in first set and from 6 to 10 and 0 in second) it starts drawing in first set (because randomIndex initial value is 0) and if it happens to draw 5 from first set, then it goes to second set and draws numbers from 6 to 10 and 0, if it happens to draw zero it goes back to draw from first set and so on. The number that is drawn becomes randomIndex.

  • Problem:

Code seems to ignore the second set. If it draws 5 in first sets it doesn't go to draw from the second but still moves around first. Even if I set randomIndex initial value to eg. 8 it still draws from first set only.

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

Answer by Dragate · Oct 16, 2017 at 01:34 PM

You get a randomValue at start. If it happens to be larger than 0.5 and smaller (and equal) than 0.85, randomIndex is assigned 5 and must draw from second set. But you do not get another randomValue, which means it's still in the range of (0.5,0.85]. That corresponds to randomIndex = 0 in the second set. The solution is do another randomValue = Random.value; upon entering the second set.

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 Raiio · Oct 16, 2017 at 02:02 PM 0
Share

Thank you for the solution. Thanks to you I found another problem and fixed it, but second one wasn't affecting piece of code shown here. I needed this to make procedurally generated and smoothly changing bioms and now all seems to work.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

74 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 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 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 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 avatar image avatar image

Related Questions

How can i do this for my Unity App? 2 Answers

how can i access list of sprites in my code ?? 0 Answers

How can i improve this code so the random numbers don't repeat it self over and over? 2 Answers

How can I create a random fixture generator. 0 Answers

Random Generator Instantiate Tiles C# = PLEASE HELP = 0 Answers


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