Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Random.Range confuses me..

Discussion in 'Scripting' started by nicmarxp, Mar 10, 2018.

  1. nicmarxp

    nicmarxp

    Joined:
    Dec 3, 2017
    Posts:
    404
    I have a simple script to make a character walk to a random waypoint in a list.

    NextWaypoints is a list, that can have either 0, 1 or 2 values, which is the same as NextWaypoints.Count if I'm not mistaken. If NextWaypoints.Count it's 2, it should pick one of the two. So either 0 (first item) or 1 (second item).

    However the code below always sets random to 0.

    Code (CSharp):
    1. if (NextWaypoints.Count>0) {
    2. int random = Random.Range(0, NextWaypoints.Count - 1);
    3. print(random + " / " + NextWaypoints.Count + "(Random between 0 and " + (NextWaypoints.Count - 1));
    4. currentWaypoint = NextWaypoints[random];
    The only way I can fix it, is to change this line to:
    Code (CSharp):
    1. int random = Random.Range(0, NextWaypoints.Count);
    But this confuses me. Shouldn't that return a value between 0-2, so 0, 1 or 2?

    When I just use print(Random.Range(0,1) it works and returns 0 or 1. Why doesn't it when I use the Count from a list?
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    I'm not sure but you might be bumping up against the difference between Random.Range with a float or with an integer. One has an inclusive max value, the other an exclusive max value. In other words, if you use integers, the last value won't be chosen. Check it out here:
    https://docs.unity3d.com/ScriptReference/Random.Range.html

    I actually complained about it on one of these forums because it took me quite a while to debug what was going wrong with a program. It's inconsistent, and the worst is, they tell you about the integers second, so I just assumed integers were inclusive for max value. One of those times my skimming got me in trouble.
     
    Last edited: Mar 10, 2018
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,399
    It doesn't; it only returns 0. System.Random works like that also (it's fairly common for random integer ranges to be upper bound exclusive).

    --Eric
     
  4. Internetpolice

    Internetpolice

    Joined:
    Oct 16, 2017
    Posts:
    60
    In unity Random.range does not return the top value you set into it if it's a list or array call (since their initial reference value is 0 rather than 1) for ease of putting in list.count and array.legnth calls. Therefore remove your count-1 and set as just count then you should be in buisness!
     
  5. nicmarxp

    nicmarxp

    Joined:
    Dec 3, 2017
    Posts:
    404
    Allright, thanks for clearing that out for me. Seems to have missed that in the docs. I tried (int) to force it to use the int version, but apparently that wasn't the issue :p