This is currently what I’ve written
float spawnXPosition = columnPool[currentColumn].transform.position.x + (UnityEngine.Random.Range(columnMin, columnMax)/ 2);
This is currently what I’ve written
float spawnXPosition = columnPool[currentColumn].transform.position.x + (UnityEngine.Random.Range(columnMin, columnMax)/ 2);
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
}
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;