switch case vs Random.Range

So, this is my code

switch(Random.Range(0,1))
{
    case 0: anim.SetBool ("SlashLowCombo", true); break;
    case 1: anim.SetBool ("SlashHighCombo", true); break;
}

It just return case 0, in 100 test time, so i had fixed to

int a = Random.Range(0,1)
switch(a)
    {
        case 0: anim.SetBool ("SlashLowCombo", true); break;
        case 1: anim.SetBool ("SlashHighCombo", true); break;
    }

And it doesn’t work to :((
I can use if else but i intend to write about 20 case >.<

(int)Random.range never return upper bound. Random.Range(0,1) will return always 0, Random.Range(0,2) will return 0 or 1.

If you have 20 cases your code might get too much sphagetti. You might use a scope in which your bools in mecanim will have standarized names like Attack1, Attack2,

Than you just use :

int number = Random.Range(1,20);
anim.SetBool ("Attack"+number, true);

two lines of code instead of infinite case.)

Random.Range provides two versions: one which handles int values, and another which handles float values.

When it comes to floats, you give a min and max, and get a random number back. Simple!

When it comes to ints, you give a min and max, and get a number between min and max-1 back. Why the “off by one”? It’s intended to make it easier to use for certain cases, but in your case it looks like it’s doing the opposite.

So, Random.Range(0,1) is effectively asking for a random number: either zero or zero. Oops. Luckily, now that you know that, it’s an easy fix:

switch (Random.Range(0,2)) //returns either 0 or 1

When it comes specifically to a “coin toss”, I sometimes do something like this:

float chance = 0.5f; //chance of heads
bool heads = Random.value < chance; //did we flip heads?

You don’t usually need that temp variable “a” to hold the value, unless you’re going to use it again or it makes things more readable for you.