# 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);`

**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
}
```

**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;
```

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

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

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

is there simple way to put random sprite to gameobject multi child with no repating. 1 Answer

how to Spawn prefab infront off player and destroy it when hr passed it 0 Answers

Color Randomize when object spawn help 0 Answers