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
1
Question by shaaafeee · Jan 29, 2020 at 04:04 PM · gameobjectrandomrandom.range

How do I check for random range which is divisible by 2

This is currently what I've written

float spawnXPosition = columnPool[currentColumn].transform.position.x + (UnityEngine.Random.Range(columnMin, columnMax)/ 2);

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
0

Answer by SparksCool · Jan 29, 2020 at 09:10 PM

i would imagine it could be something like this:

         float value = Random.Range(0, 1000);
         if(value / 2 != Mathf.RoundToInt(value / 2))
         {
             // Do Stuff
         }
         else if (value / 2 == Mathf.RoundToInt(value / 2))
         {
             // Do more Stuff
         }

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

Answer by orionsyndrome · Feb 11, 2020 at 02:59 AM

Divisibility-by-two check doesn't make sense in the context of real numbers. Of course it is divisible by two. Any real number is divisible by two, ad nauseam.

What you probably need is whether its integer part is divisible by two.

Also you shouldn't check by comparing the two float values for equality (as suggested by another answer). That's something you simply don't do, because it's unreliable due to rounding errors.

If you really need to compare two floating point numbers, use Mathf.Approximately, or subtract the two numbers and check whether their absolute difference is greater than some sufficiently small number called an epsilon (i.e. Mathf.Abs(value2 - value1) < epsilon where epsilon could be 1E-6f).

Secondly, what you need is not divisibility-by-two, but whether the number is even (which is, arguably, the same thing, at least in practice).

This is how you do it:

 bool IsEven(int value) => value & 1 == 0;

Now, why this works might not be apparent for you, but it's because how integers are stored and represented in memory (and anywhere else). Consequently, any odd integer has its least significant bit turned on, and if that was the case, this check would return false. This is btw the best and the fastest way to determine whether an integer is even or not. If you want to apply this to floating point numbers, cast them to int beforehand (which will truncate the fractional part).

Your question was "How do I check for random range which is divisible by 2"

I'm not sure I fully understand it, but Random.Range works with integers as well, like so

 Random.Range(0, 4); // will return 0, 1, 2, or 3

Ultimately, all you have to do is to test this value:

 int value = Random.Range(0, 100);
 Debug.Log("Number " + value + " is " + (IsEven(value)? "even" : "odd") + ".");

As I said, I didn't quite understand your question, but if you wanted to proactively produce only even random values, in that case you could've preemptively treated Random.Range like this

 // this will return a value in a min, max range that is guaranteed to be even
 // while ignoring the odd ones, simply because all even integers are n = 2k
 // (and by extension, divisible by two)
 int value = 2 * Random.Range((min + 1) / 2, (max + 1) / 2); // note that this is integer division

 // alternatively, bit shifting will achieve the same thing
 // though I can see this might be harder to grasp for some
 int value = Random.Range((min + 1) >> 1, (max + 1) >> 1) << 1;

And so

 int RandomRangeEvenOnly(int min, int max) => Random.Range((min + 1) >> 1, (max + 1) >> 1) << 1;
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 orionsyndrome · Feb 11, 2020 at 07:14 PM 0
Share

it just occurred to me that the last two code boxes might not work as intended if $$anonymous$$ and/or max are negative integers. I haven't tested it, but if that's the case, just ping me.

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

264 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 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 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 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

Use all variables from array (in Random.Range()) 2 Answers

What range of values need to be inserted if we need to pull a random item from the list? 0 Answers

objects of same parrent random scale/size(xy and z) 0 Answers

How to get random numbers for prefab objects in scene? 0 Answers

Random value at awake? 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