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;
@Kurt-Dekker has the solution I was trying to get you to. I just forgot about Random.value. So, there you go!
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!");
}
oh. yeah my bad. it is obviusā¦ basicly it saying if random.value < 0,5f return true.
again. thanks.
@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ā¢.
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