Unity Random Bool with three different methods

Do you need to create a random Bool or Boolean in Unity? Here is 3 different methods to show you how to do Unity Random Bool. Bool can also be seen as a number. In several languages, 0 is considered false, and 1 is considered true. Let us see how to do it. A C# random bool is quite easy. But do we do it right?

What could we use it for? Random true or false questions and answers is one thing. Random events. Do we want an event to trigger by random with only those two conditions? Then this might be for you.

Unity Random Bool methods

Method 1

The first method is the easiest to understand if you are new to Unity or C#. Maybe you also came up with a similar method. There are easier and more efficient ways, but none the less here it is. This example creates 5 random bools and prints them in the Unity console.

using UnityEngine;

public class RandomBool : MonoBehaviour
{
    void Start()
    {
        for (int x = 0; x < 5; x++)
        {
            int a = Random.Range(0, 2);  // Random number from 0 to 1
            bool Boolean;
            if (a == 0) // if a is 0 make the bool false
                Boolean = false;
            else.       // if a is not 0, make the bool true
                Boolean = true;
            Debug.Log(Boolean); // Print out the result
        }
    }
}

Method 2

The next method might be a bit more complicated to understand. It is short and more efficient than method 1. Again we will create 5 random booleans and print out the result.

using UnityEngine;

public class RandomBool : MonoBehaviour
{
    void Start()
    {
        for (int x = 0; x < 5; x++)
        {
            bool Boolean = Random.Range(0, 2) != 0;
            Debug.Log(Boolean);
        }
    }
}

If we look inside the for loop. First, we create the bool variable and call it Boolean. We then find a random number, either 0 or 1. If the random number is not equal (!=) to 0, the bool is true.

Also if it is 1 the bool is true, if it is 0 the bool is false. It is basically the same as method one but in a very short form.

Method 3

The last Unity random bool method is easier to understand than method 2 for beginners. Like with the previous two methods we create 5 random bools and print out the results.

using UnityEngine;

public class RandomBool : MonoBehaviour
{
    void Start()
    {
        for (int x = 0; x < 5; x++)
        {
            bool Boolean = (Random.value > 0.5f);
            Debug.Log(Boolean);
        }
    }
}

First, we create a bool that we call Boolean. Then we get a random value between 0 and 1. If the numbers are above 0.5 the bool will be true. Below 0.5 and it will be false. Random.value generate a float value between 0 and 1 and will looks something like this: 0.8204793

This is probably the most efficient method out of the three methods presented here. Personally I would use method 2 or 3 for a Unity random bool.

Looking for creating random numbers in Unity?

About Author

Related Posts

Unity persistant datapath

Unity Persistent Data Path

The Unity persistentDataPath is read only and will return the path where you can save data that will be persitent betwen sessions. Even app updates doesn’t touch…

Unity Random Number

Unity random number with examples

How to do a Unity random number for integers and float. Let us go thru a few examples with random integer and random float using unity random…

unity animation samples not showing

Unity animation samples not showing in Unity 2019.3

Unity animation samples not showing? Relax, they did not remove it. They have just hidden it. Not sure why it never bothered me when it was there…

unity curl error 7

Unity Curl error 7

I recently had this error in Unity. Unity Curl error 7: Failed to connect to cdp.cloud.unity3d.com port 443: Connection refused For a few days, I had been…

Unity call function every 0.1 seconds

Unity Call function every 0.1 seconds

In Unity call function every 0.1 seconds. This can be done in several ways. I will show two ways of calling a function every 0.1 seconds. Unity…

Unity Depth Charges

Unity Depth Charges “Remake” with source code

My Unity Depth Charges remake is not an exact remake. I took the idea of a destroyer dropping depth charges to bomb submarines. Other than that there…

Leave a Reply