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 range. To get a random number is an important part of almost every game.

Unity random number: integer

The range of int we will use in this example is from 0 to 10. We want to include 10 as a possible number to get. Let us draw 9 random numbers and have a look to see what they look like. I have created an empty game object to the scene and attached this little script.

Here is the script to create unity random number from 0 to 10.

using UnityEngine;

public class RandomNumber : MonoBehaviour
{
    void Start()
    {
        int a;

        for (int x = 0; x < 10; x++)
        {
            a = Random.Range(0, 11); // min = 0, max = 11
            Debug.Log(a);            // return numbers from 0 to 10
        }
    }
}

This is what our output looks like.

If you run this several times you will get a different result every time. If you need to have the same procedure of random numbers, you will need to check out Random Seeds.

How to get float instead of integer

We will use the same project as we only need to make a small change in the script to make it generate random floats instead of integers. We also change the max number to 10 instead of 11. Here is the updated script snippet.

using UnityEngine;

public class RandomNumber : MonoBehaviour
{
    void Start()
    {
        float a;

        for (int x = 0; x < 10; x++)
        {
            a = Random.Range(0f, 10f); // Min = 0, Max = 10
            Debug.Log(a);              // returns numbers from 0 to 10
        }
    }
}

We just declare the variable as float instead of integer. And add an f to the numbers in the Random.Range method. That will give random floats instead of integers. Lets look what kind of output this will give us.

Syntax

This little example will find numbers in the same range. Remember when using integers you have to add 1 to the max numbers. That is the most important difference when using a random range in Unity.

using UnityEngine;

public class RandomNumber : MonoBehaviour
{
    void Start()
    {
        float a = Random.Range(0f, 10f);
        int b = Random.Range(0, 11);
    }
}

This is how Unity random numbers work using the random range method. Check out the official Random.Range documentation.

Check out more unity relate articles.

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 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…

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…

This Post Has One Comment

Leave a Reply