Skip to content

[Unity] How to Use Random to Generate Random Numbers

Last Updated on 2021-10-29 by Clay

The random numbers can be seen everywhere in a game, regardless of the character's ability or the enemy location, we can all use random numbers to determine.

So, how to use random numbers through Random module in Unity is a very important matter.


How to use Random

In below, according to different functions, different usage methods of Random are introduced.


Random.seed set random seed

Random.seed = 777;


If you do not set this setting, Unity‘s default setting is to take the system time as the random number seed.


Random.Range(INT, INT) generate random Integer number

This is the most common method to get random values in Unity. We have to pass two integer values to Range(), which are the minimum and maximum values of the range.


Random.Range(FLOAT, FLOAT) generate random Float number

In fact, it is very similar to the integer value generation above, but the range we use floating point number (float) instead of integer number.


References


Read More

Tags:

Leave a Reply