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 tristanlbailey · Sep 29, 2016 at 02:23 PM · scripting problemrandom.range

Random Range function changed?

I recently updated Unity to version 5.4.1 from 5.3.1, and I noticed that in one of my animation routines, one animation clip was no longer being played. Upon inspection, I discovered that one array element out of an array of four animation clips was not being played any longer (specifically, the last element). The script that I have attached uses the Range.Random function (integer version), to randomise which animation clip is played.

 v = Random.Range (0, 3)
 animator.SetInteger ("AnimIndex", v);

which I have since adjusted to:

 v = Random.Range (0, 4)
 animator.SetInteger ("AnimIndex", v);

The animations are now working (completely) again. Note that I have not added any new animations in since the update.

Question: Has the integer version of the Random.Range function been changed to have an exclusive max index, rather than an inclusive one? If so, was this to provide consistency over both integer and float versions of the function? Secondly, why was it not mentioned in the release notes for the latest update, or updated in the documentation?

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

2 Replies

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

Answer by stepan-stulov · Sep 29, 2016 at 02:59 PM

Int version has always had an open right end. I personally don't find it inconsistent. Int and float are conceptually different. But you are right, not very intuitive.

What you're specifying is a range of integers among which you want a random pick with a uniform probability distribution. Imagine you'd specify a range [1..3]. Although you kind of give 3 points on the number line you actually only split this segment into 2 sub-segments. And a randomly chosen point on that whole segment is being floored down to its left end. So everything between 1 and 2 becomes 1 and everything between 2 and 3 becomes 2. This is of course highly arguable.

In your case Random.Range(0, 3) will produce 0, 1, 2.

It's also conveniet in conjunction with lists/arrays, you don't have to do -1:

 elements[Random.Range(0, elements.Count)]

PS: I myself got confused with this many times. I think most frequently what happens is people forget to write 0.0F instead of 0 and the other function is used implicitly.

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 tristanlbailey · Sep 30, 2016 at 03:32 AM 1
Share

Perhaps "inconsistent" wasn't the best word to describe it. Yes, the differences are not intuitive, as people have been confused quite a lot with this particular function in the past, it seems.

Thanks for your answer.

avatar image
1

Answer by ScaniX · Sep 29, 2016 at 02:47 PM

I don't think this was changed (I am using 5.3.5 and it is exclusive there as well) and the documentation clearly states that the max value of the int version is exclusive:

https://docs.unity3d.com/ScriptReference/Random.Range.html

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 tristanlbailey · Sep 30, 2016 at 03:27 AM 0
Share

You're right. It appears I got the two versions mixed up. Coding late at night probably doesn't help, though I'm definitely not the first to have this problem with this particular function.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Cannot read a random integer from another script. 1 Answer

Different random number - Same script - Different cloned object 0 Answers

continously spanws and overlaps 0 Answers

How to generate a random color? 5 Answers

continuously spawning and causes spawned objects to overlap 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