# Unity Random number Generation

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 a random seed is and how to generate random int and random float in Unity with code samples.

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 number generators. 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. What the left shift and the right shift do is 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?

You can simply set the random seed using the function “random.seed”. 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.seed = 1534;
}``````

## Unity Random Number generation

Generating integers and floats are the two major uses of Random number generators in unity. 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.

### Unity Random Int

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

### Unity Random Float

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

### Random.value

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

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.