Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved Random bool?

Discussion in 'Scripting' started by Alnos, Jul 2, 2021.

Thread Status:
Not open for further replies.
  1. Alnos

    Alnos

    Joined:
    Aug 20, 2019
    Posts:
    110
    So I know of Random.range for int and float, but is there somethin similar for bool value?
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,191
    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.
     
  3. Alnos

    Alnos

    Joined:
    Aug 20, 2019
    Posts:
    110
    yeah I though about that and already tried it. visual studio give me an error. it say " cannot implicitly convert type'int' to 'bool' "
     
  4. Alnos

    Alnos

    Joined:
    Aug 20, 2019
    Posts:
    110
    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.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,005
    Code (csharp):
    1. bool coinIsHeads = Random.value < 0.5f;
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,191
    @Kurt-Dekker has the solution I was trying to get you to. I just forgot about Random.value. So, there you go!
     
    Kurt-Dekker likes this.
  7. Alnos

    Alnos

    Joined:
    Aug 20, 2019
    Posts:
    110
    thanks guys.
     
  8. Alnos

    Alnos

    Joined:
    Aug 20, 2019
    Posts:
    110
    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.
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,005
    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:

    Code (csharp):
    1. if (1 < 2)
    2. {
    3.   Debug.Log( "One is less than two!");
    4. }
     
    Ryiah and Bunny83 like this.
  10. Alnos

    Alnos

    Joined:
    Aug 20, 2019
    Posts:
    110
    oh. yeah my bad. it is obvius... basicly it saying if random.value < 0,5f return true.

    again. thanks.
     
    Bunny83 and Kurt-Dekker like this.
  11. Alnos

    Alnos

    Joined:
    Aug 20, 2019
    Posts:
    110
    @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.
     
  12. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,005
    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(tm). :)
     
    Last edited: Jul 4, 2021
    the_real_ijed and Alnos like this.
  13. Nefisto

    Nefisto

    Joined:
    Sep 17, 2014
    Posts:
    338
    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
    Code (CSharp):
    1.     int a = 1;
    2.  
    3.     if (false && a++ == 1)
    4.     {
    5.  
    6.     }
    7.  
    8.     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

    Code (CSharp):
    1.  
    2.     if (false && MyMethod())
    3.     {
    4.  
    5.     }
    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
     
Thread Status:
Not open for further replies.