Hi,
Documentation says Random.Range is exclusive on its max value but when I try it on 5.1.1f1, it doesn’t return it’s max value so it’s inclusive.
Can you guys confirm it?
Hi,
Documentation says Random.Range is exclusive on its max value but when I try it on 5.1.1f1, it doesn’t return it’s max value so it’s inclusive.
Can you guys confirm it?
It depends if you are using ints or floats. Although in Unity 4.67f1, code documentation states it is inclusive for ints but the online documentation states it is exclusive => test reveals it is exclusive
using UnityEngine;
using UnityEditor;
public class TestEditor : MonoBehaviour
{
// Random.Range( int, int ): code docu states it is inclusive, online docu says it is exclusive
[MenuItem("Tools/Test/Is Random.Range( int, int ) inclusive or exclusive")]
static void TestRandomRangeInt()
{
const int TRIES = 1000000;
bool isInclusive = false;
for( int i = 0; i < TRIES; i++ )
{
int r = Random.Range( 0, 2 );
if( r == 2 )
{
isInclusive = true;
break;
}
}
if( isInclusive )
{
Debug.LogWarning( "Random.Range( int, int ) is inclusive!" );
}
else
{
Debug.Log( "After " + TRIES + " tries Random.Range( int, int ) was not inclusive" );
}
}
}
it doesn’t return it’s max value → it excludes it → it’s exclusive
there are two version of Random.Range. one are returning float and for int. just look at it again and scroll down.
technically both of it for maximum value are exclusive, and since float number can get decimal value it technically impossible to reach maximum value (unless minimum is same as Maximum).