A Suggestion for the Random.Range Slide

I think it makes more sense to display it as Random.Range(min, length) on the slide, rather than Random.Range(min, max). I realize the exclusivity is explained on the slide (and briefly discussed) , but I believe changing the parameter name on the slide will help beginners understand the concept better.

That might make it more confusing given the min is not always 0, so length and max are not always the same.

I wasn’t implying that min is always 0. Min is just our starting index. I’m just stating that the max parameter initially made me think that we are telling range to look for all numbers between min and max, with both being inclusive. But it’s really saying start at index min and go for length max.

Actually, it’s NOT min, Length, it literally is Min, Max.

In floats, Random.Range will always return a number between Min and Max, so Random.Range(2f,3f) will return a number between 2 and 3.

In Ints, Random.Range will always return a number between Min and Max-1, so Random.Range(10, 20) will return an integer could return 10,11,12,13,14,15,16,17,18,19

@Brian_Trotter I wasn’t aware of how the float range works. Thanks for clearing it up for me. I now agree min max are the best suited parameter names. One last question though, why does int range do max-1?

Because of the way .Count and .Length work in lists and arrays…
If you have 10 elements in an array, they are accessed from 0 to 9… (a quirk in the programming world, so if you want the randomth element in a 10 element array, you want a number between 0 and 9… So Random.Range(int, int) was written in such a way that you can pass it (0, mylist.Count) and never get an out of bounds error.

This is the answer I was looking for. @Brian_Trotter you’re the man!

1 Like

Privacy & Terms