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 /
avatar image
0
Question by EternalAmbiguity · Jan 17, 2017 at 08:16 AM · randomfloatrandom.rangeprecision

How Precise is Random.Range?

If I use Random.Range (0f, 1f), how precise is it? Or how many different values can it return? How many decimal places does it go out to?

And, is it dependent on the number of decimal places I use in my min and max values?

Edit: And does it change based on the size of the range? Does a range of (0f, 100f) have more or the same number of possible return values than the previous situation?

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

Answer by tanoshimi · Jan 17, 2017 at 08:36 AM

Random.Range returns a floating point value, so the concept of decimal places doesn't really apply.

In terms of the range of possible values, a single-precision float is 32bit and each of those bits can be 1 or 0. So that's 2^32 different values that it can represent = 4,294,967,296. Now by asking only for output within a given range you won't get that many unique outcomes because you're applying a constraint on the exponent, but you should get enough variety for almost all game applications.

Comment
Add comment · Show 10 · 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 EternalAmbiguity · Jan 17, 2017 at 03:34 PM 0
Share

So would 1/4294967296 for the first one give the first possible value, with all others being multiples of that? And 100/4294967296 for the second one? Or am I understanding it incorrectly?

Additionally, you say that one won't get many unique outcomes...shouldn't it it be random, regardless?

avatar image NoseKills EternalAmbiguity · Jan 17, 2017 at 04:41 PM 1
Share

You have to understand how floating point numbers work (at least a little bit) to understand the answer. It's not that straight forward...

"Floating point" means that the position of the decimal point...wait for it... "floats". With bigger numbers you have less decimal precision since the large whole number part takes up more bits.

Additionally, you say that one won't get many unique outcomes...shouldn't it it be random, regardless?

@tanoshimi said

Now by asking only for output within a given range you won't get that many unique outcomes

A range of 0 to 1 has fewer possible random unique bit combinations than the whole range of float.$$anonymous$$inValue to float.$$anonymous$$axValue which contains all of the possible combinations.

avatar image Glurth EternalAmbiguity · Jan 17, 2017 at 04:59 PM 1
Share

I like to think of floats the same way I think about scientific notation, like 5.234E-4= 5.23x10^-4

Though obviously it's is all base-2, not base 10, like the E above represents. the exponent part of a 32 bit float is 8 bits, representing a range from -126 to +127 (0xff has special meaning, so not included in the range )

The decimal part (the mantissa) defines the precision: It is 23 bits, and assumed to start with 1.X (the 1 BEFORE the decimal is not stored, just assumed), for a total of 24 bits of precision. This means that the least-significant-digit of the mantissa has a precision of: 1 part in 2^24 (or 0.00000011920928955078125 in decimal).
In other words- for any given exponent, there are 2^24 (about 16.7 million) different possible decimal values.

avatar image EternalAmbiguity EternalAmbiguity · Jan 17, 2017 at 09:13 PM 0
Share

Okay, I think I understand it a bit more. The value 1.4123124234... can go out to further amounts of decimal places than the value 14123124.234...in the end though, both will have the same number of significant figures (if we assume all numbers are non-zero, because zero's a pain in sig figs).

If that's not right please let me know.

A range of 0 to 1 has fewer possible random unique bit combinations than the whole range of float.$$anonymous$$inValue to float.$$anonymous$$axValue which contains all of the possible combinations.

In this case, should one assign the $$anonymous$$ and max values? Can one even do that?

Basically, I'm planning to have a set of probabilities, say p=0.0000015 to start. Then Random.Range will run. Then if the result is <= 0.0000015, some action would happen.

I can probably multiply by some constant if that helps somehow, but will using Random.Range(0f, 1f) work to produce truly random results? How can I make it more "accurate" or "realistic?"

avatar image Glurth EternalAmbiguity · Jan 17, 2017 at 09:37 PM 1
Share

"go out to further decimal places": well- yes and no, depend on exactly what you mean. I think yes, the way you mean. but no as far as the way it's stored: 1.xxxxx(23bits) x 2^yyy(8bits)

When working with probability and random: rather use 1.0 to define the maximum probability, I'd use uint.$$anonymous$$axValue as the maximum. All my probabilities are then stored, and generated as unsigned integers. In fact, this will give you a greater (easily) useful range of different probabilities (32 bits, rather than the 23 used by the mantissa of a float: uint range: 0 to 4,294,967,295 ) edit: oh, looks like random range only does signed int, (or float), still you could probably just cast the result to an unsigned int.

Show more comments

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

6 People are following this question.

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

Related Questions

Need help with generating random Multiplier,Need help with generating random multipliers 0 Answers

Random.range multiple instantiations without repetition 1 Answer

Randomising list of integers and removing them from list once the task assigned to the integer in the project is completed 0 Answers

reload random scene 2 Answers

Randomly setting a boolean to true? 3 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