September 21, 2020

soltveit.org

..Geekish..

Unity Random Bool with three different methods

Unity Random Bool

One thing is a random number in Unity, and sometimes when making games we might need a 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?