- Home /
random.range not working
in my project im making a wandering AI, it rotates a random amount with this transform.Rotate (0, 0, Random.Range(-2, 2)); however it always rotates negative unless I make the max range 3 or more, how can I fix it so each number has an equal chance of working?
Answer by unity_ek98vnTRplGj8Q · Jul 23, 2020 at 07:39 PM
https://docs.unity3d.com/ScriptReference/Random.Range.html
You are using the integer version of Random.Range which is inclusive on the lower bound and exclusive on the upper bound, meaning Random.Range(-2, 2) has a possibility of selecting -2, -1, 0, or 1 with equal probability. If you change the upper range to 3, then Random.Range(-2, 3) has a possibility of selecting -2, -1, 0, 1, or 2, which is what you want.
Alternatively, if you don't want it to select only whole numbers and rather have it use any value between -2 and 2, you could use the float version of Random.Range by calling Random.Range(-2f, 2f)
Your answer
Follow this Question
Related Questions
Bug with my enemyAI 2 Answers
Wandering enemies 1 Answer
random target selection 2 Answers
2d enemy prefab (enemy movement and instantiate) 1 Answer
Random position inside multiple areas 2 Answers