Why Random.Range why??

I don’t know when this changed but I thought I’d share with everyone…

Random.Range has recently changed as it now bugs our who game, I guess in 5.4
Random.Range became inclusive at both ends even in the integer version.

but of course the documentation is not reflecting that:

no biggy, it’s just going to break things for anyone who depended on the exclusive nature to access collections by random index…

oh wait :hushed:

What version exactly are you on? I’m not getting that in 5.4.0.f1

EDIT: I lied, it’s f3

Can you post your UnityEngine.dll so we can inspect it? :smile:

@Baste 5.4.0f3
@jimroberts reverse engineering is bad XD

How is it bad? Genuinely curious to see if the implementation is actually different or if your IDE is simply displaying bad documentation. There is nothing listed in the release notes about a change for Random.Range…

Very weird, I get the following in Unity 5.4.0f3:
2747183--198075--upload_2016-8-10_15-19-27.png

Looks like it’s showing the description for the wrong overload. OP’s says it returns a random float, while @Timelog 's says it returns a random int. Probably just an IDE bug.

Yup, It’s also showing up as exclusive for me. I was wrong earlier, I’m on f3 too. Also ran a test where it’s exclusive.

It’s probably just MonoDevelop being broken because MonoDevelop is always broken. Hell, if you asked me “do you think MonoDevelop would show the docs for the correct overload” before this thread, I’d say that the safe bet would be “no”

I saw the max being included in debug too which is also weird. I might just restart everything and see if there’s some sort of bad reference happening

@jimroberts

still happening

2747200–198077–UnityEngine.zip (357 KB)

Tested it with another 3 devs in the office and it happens to all of them :confused:

The docs are found in UnityEngine.XML, which is next to the .dll

Open it, and search for Random.Range, see the one for int.

Looking at your original post, note that it says “float” in the description, you’re definitely getting the float one. The question is if there’s a bug where the doc-string is broken, or if it’s an MD bug.

I mean, it’s an MD bug since MD is utter garbage, but you can check for yourself.

Yeah you’re right, scary if that ever happens as we’ll be getting bugs here and there :confused:

Unfortunately Random.Range is actually calling a function on the C/C++ side of the engine so we can’t see the actual implementation. Post the results of the following code if you want to be sure…

int inclusiveMaxCount = 0;
for (int i = 0; i < 1000000; ++i)
{
    if (Random.Range(0, 1) == 1) { ++inclusiveMaxCount; }
}
Debug.Log("Detected " + inclusiveMaxCount + " instances of an inclusive max range.");

Yeah I just did and got 0 on everything so it means it’s the IDE’s/MD’s bug.

Time to switch to Visual Studio :smile:

We’re all using VS!

I actually delete the monodevelop folder each time i install unity, it’s like a ritual by now

So your screenshot isn’t white because you’re using MonoDevelop, it’s white because you’ve actually chosen to use the horrible default VS color scheme?

Why do you hate your eyes so? What did they do to you?

That’s definitely a bad bug. Is VS Tools for Unity installed? Did you install it with the installer or manually?

5 Likes

Haha, I know what you mean but I could never get used to the dark theme. I tried, honest! 10 years later, still the same.

I installed them through the installer, haven’t done it manually for some time