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
1
Question by 237641 · Oct 26, 2014 at 11:01 PM · c#random.range

Changing the range of Random.Range during runtime - C#

I want to change the range of a random number generator as time passes making it more likely that the generator chooses a certain number making my game less based on luck. For some reason my generator doesn't seem to acknowledge the change in the max value and continues to return values according to the previous range. Kind of hard to format in this text box but these are single line if statements and therefore I didn't use brackets. These are under the void Update() method. The if statements systematically change the upper range of the random class so there is a greater probability of the number "50" being chosen. Since I have randomNumber as public I can see I in the inspector and it seems to still adhere to the old range. Also I can see randomRange changing; it just doesn't seem to make a difference to the Random.Range method. Code:

  if ((Time.time >= 10) && (randomRange == 500))  
          randomRange = 400;
  if ((Time.time >= 120) && (randomRange ==400))
            randomRange = 300;
   if ((Time.time >= 240) && (randomRange == 300))
          randomRange = 100;
   randomNumber = Random.Range(0, randomRange);
   if (randomNumber == 50)
             //the code I want to execute.. Not relevant to question




Comment
Add comment · Show 5
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 Habitablaba · Oct 26, 2014 at 11:03 PM 0
Share

Please format your code.

avatar image richyrich · Oct 27, 2014 at 01:16 AM 0
Share

It's unclear whether those are intentional if's, nested if's or intended (but forgotten) else-if's. Therefore its difficult for anyone to assist further. As Habitablaba says, sort the formatting out please.

avatar image 237641 · Oct 27, 2014 at 11:31 AM 0
Share

They aren't else -ifs because only one will be true at any given time. The code just runs down checking to see if any of them meet the requirements. If they do the code it executes will prevent it from ever running again. Range changes perfectly but the Random.Range still returns values in the same range

avatar image richyrich · Oct 27, 2014 at 11:40 AM 0
Share

Ok. So it IS intended that on some occasions, every single line above will be executed one after the other

avatar image Unitraxx · Oct 27, 2014 at 12:09 PM 0
Share

This looks O$$anonymous$$, I think we need more code.

1 Reply

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

Answer by Bunny83 · Oct 27, 2014 at 12:17 PM

Well, if you see that randomRange changes, then your Random.Range call will use the new range. You said it seems that it doesn't use the new range, what have you done to verify that? Is randomNumber a public variable? If not, make it public so you can see it in the inspector. Once randomRange is 400 there shouldn't be any number greater or equal to 400.

Do you just have the "feeling" it doesn't change anything? That's probably because there's almost no difference between 1 in 500 or 1 in 400 or 1 in 300. Even 1 in 100 can take ages to get one specific value. It's random, that's the point, isn't it? You increase the possibility slightly but that doesn't mean that you see an instant effect. Random events like this can't be predicted in any way. The probability is only the average across infinite uses.

If you just want to have different spawn delays which vary randomly you should pick one random number and use it as delay value. For example:

 float nextEvent = 0;
 float randomRange = 60f;  // start with 30 - 60 seconds delay
 
 void Update()
 {
     if (Time.time > nextEvent)
     {
         if (Time.time > 240)
             randomRange = 10f;    // 10 - 20 seconds delay after 4 minutes
         else if (Time.time > 120)
             randomRange = 30f;    // 15 - 30 seconds delay after 2 minutes
         else if (Time.time > 10)
             randomRange = 40f;    // 20 - 40 seconds delay after 10 seconds
 
         nextEvent = Time.time + Random.Range(randomRange/2, randomRange);
         // Do your "not relevant" stuff here
     }
 }

Comment
Add comment · Show 3 · 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 237641 · Oct 27, 2014 at 02:17 PM 0
Share

Yes I have the randomNumber as public and it still outputs numbers greater over 400. I know there are probably other methods to do this but I was wondering why the Ranom.Range doesn't acknowledge the change in the maximum value.

avatar image Bunny83 · Oct 27, 2014 at 09:39 PM 0
Share

That's actually not possible. Try adding a Debug.Log like this to your original code:

 randomNumber = Random.Range(0, randomRange);
 Debug.Log("Random number: " + randomNumber + " out of 0 - " + randomRange);

If randomRange is 400 you won't get values equal or greater than 400. Are you sure you use the correct variables or maybe you have another local variable named randomNumber or randomRange.

avatar image 237641 · Oct 27, 2014 at 11:02 PM 0
Share

I'm going to have to apologize. I feel like laughing at myself. The code was working properly but I had my Random.Range generator under 2 if-statements and I had only changed the number generator for the one that was not being executed. I'm going to mark your question as having answered $$anonymous$$e for your help. Thanks. Was quite a funny experience when I was copying my source code into a word processor and I noticed that the former if-statement wasn't changed to include randomRange.

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

29 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Why is Random.Range exclusive for integers but inclusive for floats? 4 Answers

My Script Wont work? 2 Answers

Creating the After Effects Wiggle expression to effect rotation in C# 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