- Home /
Problem with Random.Range
Hi everyone! I want to create an object, but just if the random.range(0,1) is 1. If it's 0, then nothing happens. Thank you and sorry for my bad english! Here is my code:
var cube : GameObject;
var spawnPosition : GameObject;
var timer = 0.0;
function SpawnCube() {
var tempCubeSpawner = Instantiate(cube, spawnPosition.transform.position, Quaternion.identity);
}
function Update() {
timer += Time.deltaTime;
if(timer > 5) {
var randomNumber = Random.Range(0,1);
if(randomNumber == 0) {
SpawnCube();
timer = 0.0;
}
if(randomNumber == 1) {
timer = 0.0;
}
}
}
Why is your SpawnCube function above the function update? $$anonymous$$ake it below it
Answer by TheCatProblem · Apr 02, 2015 at 06:39 PM
The upper bound of Random.Range()
is exclusive (i.e., it will never be selected). Thus Random.Range(0, 1)
will always return 0. See the documentation for more details.
You could instead use var randomNumber = Random.Range(0.0f,1.0f)
and check if randomNumber
is less than 0.5f
(which it should be roughly 50% of the time) instead of checking if it equals 0 or 1.
Correct! But it's not always excusive.
"Returns a random float number between and $$anonymous$$ [inclusive] and max [inclusive] (Read Only)."
"Returns a random integer number between $$anonymous$$ [inclusive] and max [exclusive] (Read Only)."
So $$anonymous$$lousianic, what you need to do is declaring your randomNumber variable as float and it will be all fine.
ozturkcompany, that's what I thought as well, but according to the documentation (at least, the version I linked to) the max value is exclusive for the float version, too. Of course, this could be (and probably is) just an error in the documentation.
@TheCatProblem: Yep, could be a documentation error, but in the float case it doesn't really matter since the likelihood that 1.0 is returned is very low since it's a float value.
Answer by DiegoSLTS · Apr 02, 2015 at 07:51 PM
Random.Range is overloaded, if you pass 2 int variables or values (explicit numbers WITHOUT a letter at the end) it'll return an integer random value between both values excluding the biggest one. If you pass at least one float variable or value (a number with an "f" at the end) it will return a real random value between both values, including the biggest one.
If you want your Random.Range to return 0 or 1 (and nothing in between) you should use Random.Range(0,2).
The suggestion of making the numbers float numbers and checking if they're greater than 0.5 is bad, it's a bad approach since it adds extra complexity to something that should be really simple, and if you for any change need a random value between 0, 1 and 2 in the future, you have to change that "is greater than 0.5" comparition since it doesn't make sense now.
Use the correct function for the result you want. You want random integer numbers, so use the Random.Range function that returns integer values.
In my opinion, use of the integer overload of Random.Range()
should generally be limited to the selection of an item from a list, array, or other container of known size.
For a situation where one of N
alternatives is to be selected at random, it's better practice to choose a random value in [0,1)
and check if it lies in [(m-1)/N, m/N)
for integer m
in [1, N]
. While marginally more complex, it's a better expression of the design intent (choose one of a number of equally probable outcomes).
In this case, it's only necessary to choose between two alternatives, so omitting the general logic and simply testing if the random value is less than 0.5f
suffices.