Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

(Random.value > 0.5f);

Discussion in 'Scripting' started by venom789, Jul 3, 2022.

  1. venom789

    venom789

    Joined:
    Apr 18, 2022
    Posts:
    178
    Hello everyone,
    I would like to understand how this code works:

    myBool=(Random.value > 0.5f);


    Random.value create me a random float between 0 and 1, and I ask if the value is greater than 0.5.

    Does this mean that a bool has a float value between 0 and 1, and that intermediate values are interpreted as 0 or 1??
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,013
    No. All it's asking is 'is this expression true or false'.

    Ergo, is this random value between 0 and 1 greater than 0.5? If yes, the boolean is true, otherwise it's false.

    It's more or less the same as:
    Code (CSharp):
    1. bool myBool;
    2.  
    3. if (Random.value > 0.5f)
    4. {
    5.     myBool = true;
    6. }
     
    Bunny83 and Ryiah like this.
  3. venom789

    venom789

    Joined:
    Apr 18, 2022
    Posts:
    178
    I understand what you want to tell me, except that I used it like that. And it works :

    Test you will see.

    Code (CSharp):
    1.  
    2. bool myBool;
    3.  
    4. public void()
    5. {
    6.             myBool= (Random.value > 0.5f);
    7.             if (myBool)
    8.             {
    9.              ...
    10.             }
    11.             else
    12.             {
    13.              ...
    14.             }
    15.  
    16. }
    17.  
     
    Last edited: Jul 3, 2022
  4. venom789

    venom789

    Joined:
    Apr 18, 2022
    Posts:
    178
    Maybe it's used so much that the Devs integrated it.
     
  5. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,013
    What? No. Did you listen to me at all? It's just a C# thing.

    Basically
    boolean = (boolean expression)
     
    Bunny83, Ryiah and Kurt-Dekker like this.
  6. venom789

    venom789

    Joined:
    Apr 18, 2022
    Posts:
    178
    A ok, I understand better, I don't know the boolean expression. As my code worked, I don't understand why, thanks
     
  7. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,030
    In short, the ">" sign is an operator that takes two numeric operands and returns a boolean value. So either true or false. This boolean value you simply assign to your boolean variable. Some people think that those operators somehow belong inside if statemens only. However those are just comparison operators and can be used anywhere a boolean value is required. You can think of operators like a special notation for methods. So this

    Code (CSharp):
    1. c = a > b
    would be the same as this:

    Code (CSharp):
    1. c = IsGreaterThan(a, b)
    This fictitious method "IsGreaterThan" would have a signature like this:

    Code (CSharp):
    1. public bool IsGreaterThan(float num1, float num2)
    It returns true whenever the first number is greater than the second number. That's what the
    >
    operator does.

    An if statement on the other hand just requires a boolean value and does not need to include any of those operators if you already have a boolean value. That's why if statements like this

    Code (CSharp):
    1. if (myBoolVariable == true)
    are redundant. You could just do
    Code (CSharp):
    1. if (myBoolVariable)
    instead.

    In your specific case you assign a random boolean value to your variable. Random.value returns a uniform random number between 0 and 1. Since it's uniformly distributed, half of the time the value would be greater than 0.5 and half the time it would be below 0.5. So this expression "converts" all random values greater than 0.5 to "true" and any value smaller than (or equal to) 0.5 to "false".

    I don't know how we can explain it any simpler :). Maybe you want to look up some boolean algebra basics. Also look up comparison operators if you have trouble understanding them. Those are the most essential parts of pretty much any programming language
     
    venom789, spiney199 and Ryiah like this.
  8. venom789

    venom789

    Joined:
    Apr 18, 2022
    Posts:
    178
    Thank you for the explanation, it's simple and effective.