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
3
Question by Trey3Stone · Jul 30, 2013 at 12:05 PM · c#randomrandom.range

Is Random.Range() Really Maximally Inclusive?

According to the C# documentation, "Returns a random float number between and min [inclusive] and max [inclusive]." However, after running a few tests, it appears as though the max is actually exclusive. Something as simple as Debug.Log (Random.Range (1, 4); never seems to return 4. Now is this some sort of error on my part, or is the documentation simply mistaken?

Comment
Add comment · Show 2
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 $$anonymous$$ · Jul 30, 2013 at 12:08 PM 0
Share

Try using Random.Range (0,1). $$anonymous$$aybe you are just unlucky :)

avatar image Trey3Stone · Jul 30, 2013 at 12:15 PM 0
Share

Nope, I'm only getting 0s with Random.Range (0, 1).

4 Replies

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

Answer by Psymon · Jul 30, 2013 at 12:19 PM

Only Range with float is maximally inclusive.

Range with integer is not maximally inclusive.

Unity Documentation

Comment
Add comment · Show 6 · 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 Trey3Stone · Jul 30, 2013 at 12:21 PM 0
Share

Of course! I can't believe I didn't notice that. Thank you!

avatar image sampenguin · May 20, 2015 at 07:29 PM 0
Share

Actually as of Unity 5, this is no longer the case. Both versions are documented as maximally exclusive. Haven't tested to verify but that's a subtle change that can cause some interesting statistical issues for existing code.

avatar image Stepepper · Jul 24, 2015 at 07:31 PM 0
Share

@sampenguin This is still the case.

avatar image Eric5h5 · Jul 24, 2015 at 08:25 PM 2
Share

The docs were wrong, but have been corrected since then.

avatar image KaizerGoreng Eric5h5 · Apr 15, 2016 at 03:33 AM 0
Share

It's no longer the case. Tested and verified.

avatar image me2000 KaizerGoreng · May 03, 2016 at 03:48 PM 1
Share

It still is the case and I'm on the latest Unity 5.3 build. If I write Random.Range(0, 2) only 0 or 1 get chosen

avatar image
0

Answer by Seyyed · Jul 30, 2013 at 12:39 PM

You can use this: Random.Range (0.0, 1.0)

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 Lovrenc · Jul 30, 2013 at 12:40 PM 3
Share

No point if he needs integers. He can just use range of 2.

avatar image
-1

Answer by EvalDaemon · Nov 12, 2016 at 07:58 AM

 List<int> x = new List<int>();
 x.Add(1);
 x.add(100);
 x.add(1000);
 int i;
  for(int i = 0; i < 100; i++)
             {
                 i = Random.Range(0, (x.Count));
                 Debug.Log("i=" + i );
             }

Always equals 0 to 2, never 3.

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 tanoshimi · Nov 12, 2016 at 07:59 AM 0
Share

Which is exactly expected behaviour.

avatar image Eric5h5 · Nov 12, 2016 at 08:07 AM 0
Share

What was the point of this? Also your code is quite broken.

avatar image
0

Answer by AmazinJacks · Apr 12, 2020 at 02:15 PM

EDIT: I was mistaken. Further down in the documentation page, it does clearly lay out that max for ints are exclusive, while max for floats are inclusive. I hadn't looked further.

I'm looking at https://docs.unity3d.com/ScriptReference/Random.Range.html which states quite specifically at the top of the Description:


Return a random float number between min [inclusive] and max [inclusive] (Read Only).

Note max is inclusive. Random.Range(0.0f, 1.0f) can return 1.0 as the value.


I'm using it for integers, which testing shows has an inclusive min and exclusive max. Perhaps it's different with floats, but the documentation confused me.

Comment
Add comment · 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

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

26 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

Related Questions

How can I do random choices 1 Answer

Generate a big digit with only 1 2 3 and 4,How to generate a big number that only countains 0 to 3 2 Answers

Multiple Cars not working 1 Answer

Non-repeating random number generator crashing unity 1 Answer

Need help with generating random Multiplier,Need help with generating random multipliers 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