Random bool?

So I know of Random.range for int and float, but is there somethin similar for bool value?

Bool is either true or false. Which is really just 0 or 1 when you think about it. So just do a random.range with 0-2 (using ints make the 2 exclusive) and depending on the number, use that to determine true or false.

yeah I though about that and already tried it. visual studio give me an error. it say " cannot implicitly convert typeā€™intā€™ to ā€˜boolā€™ "

if thereā€™s no quick fix, iā€™ll just create an int variable instead, but still iā€™m curious if thereā€™s a way to generate a random bool.

bool coinIsHeads = Random.value < 0.5f;
6 Likes

@Kurt-Dekker has the solution I was trying to get you to. I just forgot about Random.value. So, there you go!

1 Like

thanks guys.

why does the " <0,5f" change it from float to bool? sorry but iā€™m still a noob, I see that it work but donā€™t understand why.

The ā€œless thanā€ operator ā€œ<ā€ takes two scalar quantities (one on each side) and returns a bool.

Itā€™s the same reason why an if statement takes a bool, and why this works:

if (1 < 2)
{
  Debug.Log( "One is less than two!");
}
2 Likes

oh. yeah my bad. it is obviusā€¦ basicly it saying if random.value < 0,5f return true.

again. thanks.

2 Likes

@Kurt-Dekker since we are there, whatā€™s the difference between the & and the && operator in an if statement?

from what I know and found they boh do the same thing.

Yes and no. On integers only & is valid and it is a bit-wise AND operator (google that up).

On boolean arguments, I believe there is no effective difference between & and &&. Both will AND the two bools and return another bool.

[EDIT: Please read @Nefisto post below; I am totally incorrect on the above and there actually IS a profound code execution difference, with the cascaded evaluation. I also agree with Nefisto that one should not rely on this for side effects because they are very hard to reason about.]

I prefer to use && for clarity when AND-ing two booleans together, since I cut my teeth on old school C language which had harder distinctions between the two operator types and did not even support a boolean: EVERYTHING was a boolean, you just asked ā€œis count?ā€ and if count was nonzero, that was considered true, otherwise false. Itā€™s actually pretty good times and I still spend a fair amount of time coding in Good Old Cā„¢. :slight_smile:

2 Likes

Sorry to say, but it has two minor differences but one of them is about binary operations and this has not to do with conditionals, so letā€™s ignore it

In conditionals when we use && or || we are saying to c# to do a short circuit so in an && condition if the first expression is false the second one will not be tested

    int a = 1;
 
    if (false && a++ == 1)
    {

    }

    Console.WriteLine(a);

If we run above code the log will print 1, but if we change to if (false & a++ == 1) print will be 2, because compiler will verify each expression. You can think of this with a method too, the idea is the same

    if (false && MyMethod())
    {

    }

This will not run MyMethod, but if we change to & the method will run

In a real-world this isnā€™t something that we should relay to, I never saw a case where this is a necessary thing. Itā€™s more a theoretical thing to know about

5 Likes