Random number generation is used in a lot of applications. In games, random numbers are mostly used for level design to achieve the procedural generation of levels. Every random number generator uses a different algorithm.

Unity random number generation uses Marsaglia’s Xorshift 128 algorithm. Though the generated number or sequence in Unity is pseudo-random, it is mostly sufficient for the application in games. In this post, we will see the different random number algorithms, what is a random seed, and how to generate random int and random float in Unity with code samples.

**Contents**hide

## Basic introduction to Random number Generation

There two major types of random number generation. One is Pseudo Random number generation and the other one is true random number generation.

In pseudo-random, a computer algorithm or a mathematical calculation is used to generate the random number. All these algorithms and calculations use a random seed as the initial value. If you keep the random seed the same every time then pseudo-random number generators will provide a similar sequence or number every time.

True randomness cannot be achieved using a computer algorithm. Mostly true random number generators use natural data to compute the random number. Mostly, atmospherically noise is considered for this application as it is totally random.

## Unity Random number algorithm

There are many random algorithms but a few have been mostly used by programmers. Random number algorithms are compared based on the number of random numbers it can generate before repeating the sequence. Unity uses Marsaglia’s Xorshift 128 algorithm. Let’s see how Unity random number algorithm generates the random number.

### Marsaglia’s Xorshift 128 algorithm

Xorshift is the most efficient random number algorithm. It is exceptionally light and fast. That is the reason it is adopted on many platforms. Not just that, the Xorshift algorithm is better at generating random numbers than many other random algorithms.

The way Xorshift works is as the name suggests. It takes an initial value and then shifts it and then Xor the result with the initial value. The Xorshift code looks something like this

```
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
```

“^=” represents Xor operation, “<<” represents left shift and “>>” represents right shift.

The left shift and the right shift, shift the binary digits by the number of times mentioned. For example, 1<<2 will change 1 to 100 in binary. The right shift operator also works in a similar way. Xor basically gives 1 if the inputs are different and a 0 if the inputs are the same. You can refer to the Wikipedia page for more details on the Xorshift algorithm.

## Unity Random Seed

The random seed is the initial number used to start with. every random number generator requires a random seed to generate random numbers. You can actually get the same behavior from the random number generator by using the same seed. This method is very useful while generating random numbers for the procedural generation of terrains.

It is not necessary to give a random seed every time. Most of the algorithms take the computer time for random seed generation. Unity random number generator uses the seconds of the computer clock as the random seed. That is why you get a different set of random numbers every time you generate random numbers in Unity.

### How to set a Random Seed in Unity?

In the older versions of Unity, you can simply set the random seed using the function `Random.seed`

. In the new version you need to use `Random.InitState`

.

You need to be very careful while setting the random seed. If you set the random seed in update function then you will get the same number every time. You need to set the random seed in either the Start or in the awake function.

```
void Start()
{
Random.InitState(1534);
}
```

## Unity Random Number generation

Unity allows you to generate random integers and floats using its random number class. You can actually do this with a single line of code in Unity.

We have added the random int and random float codes for your reference below. Both these are achieved using `Random.Range`

function in Unity. `Random.Range`

by default takes two floats as the input. It can be overloaded with integer inputs as well.

### Generating a random integer in Unity

To generate a random integer, just pass in minimum and maximum values as intergers to `Random.Range`

function. The function includes the minimum value and excludes the maximum value in case of integer inputs.

`int randnum = Random.Range(2, 10)`

The above code will generate a random integer between 2 and 10. The output may include 2 but never 10.

### Unity Random Float

You can generate a random float by passing floats as the minimum and maximum values to `Random.Range`

. In case of random float, both the minimum and maximum values are included.

`float randnum = Random.Range(2.0f, 10.0f)`

The above code will generate a random float between 2 and 10. The output may include 2 and 10.

### Random.value

`Random.value`

generates a random float between 0.0 and 1.0. You can use this if you need a dynamic range.

The below code allows you to set a range and multiply it with `Random.value`

to generate a random float between 0 and 10.

```
Float range=10f;
float random_num= Random.value*range;
```

## Using Random class of C#

Both Unity and C# have a random Class. In the above examples, we saw how to use Unity’s random class. You can also use the C# random class, which is a part of system namespace. It’s not advisable to add the system namespace to a Unity script, as Unity will not know if you are referring to the Unity random class or the C# random class.

To use it, we can directly use `System.Random`

inside our code. Since random is a class, we need to create an object for the class to generate Random number. Here is a sample script to generate random numbers using `System.Random`

```
System.Random rand=new System.Random(); //Creating an object
Debug.Log(rand.Next(50)); //Generate Random number between 0 and 50
Debug.Log(rand.NextDouble()); // Generate Random float between 0 and 1
```

We have covered the different ways to generate random numbers in Unity. If you have any other questions on them, feel free to leave a comment below.

Very thoughtful blog

This clarified a lot on Unity Random numbers.

Great post! I appreciate the effort you put into explaining the topic in a clear and concise manner. It’s really helpful for someone like me who is new to this subject. Thank you!