# Random generated value always avoids math operation (biased)

SCENARIO: I’m trying to give an enemy a 50% chance of using a slightly different attack reach, so I’m using UnityEngine.Random.value < 0.5f

Here’s the code snippet of interest:

private void RangeCorrection()
{
if (playerRb.velocity == Vector3.zero)
{
rangeFixMultiplier = 0.0f;
return;
}
rangeFixMultiplier = (180.0f - Vector3.Angle(playerRb.velocity, transform.forward)) / 180.0f;

if (Random.value < 0.5f)
{
rangeFixMultiplier *= 2.0f;
Debug.Log("Performed");
}
else
{
Debug.Log("Avoided");
}
}

This method “RangeCorrection()” is repeatedly called in Update() because the value of “rangeFixMultiplier” variable needs to be calculated frequently. When a player moves into the enemy’s range, the MOST RECENTLY updated value of rangeFixMultiplier will be used to calculate the enemy’s attack reach (that’s either rangeFixMultiplier’s original value OR rangeFixMultiplier * 2.0f). PROBLEM: Surprisingly, rangeFixMultiplier * 2.0f will never be used because the most recently generated Random.value right before the enemy attacks will ALWAYS be >= 0.5f for some reason.

If anyone has dealt with this before, has any ideas, or needs additional details, please let me know. Thanks!

Other observations:

1. Random.value works as expected (generating float values spanning from 0 to 1) as Update() runs but it’s just that the most recently Random.value generated ALWAYS avoids the if-block.
2. If i add a “rangeFixMulitplier *= 3.0f” into the else-block, rangeFixMultiplier *= 2.0f will now always execute. (i.e., Random.value flips from being always >= 0.5f to now always being < 0.5f)
3. If I remove “rangeFixMultiplier *= 2.0f” from the if-block so that only the two Debug.Log() statements are left, Random.value works as expected.
4. If I change the threshold from 0.5f to something like 0.2f or 0.9f, Random.value will adjust itself to still avoid performing the “more significant” operation.
5. Setting an arbitrary Random seed, using Random.Range(), or using System.Random instead of UnityEngine.Random does not fix this issue.

I See only two possible reasons for this:
Either you heavily misinterpreted your observations, or you have some code elsewhere that is messing with the pseudo random number generator. My first guess would be the second point. So how well have you isolated the code that has this issue?