How Unity generates random numbers (setting the seed)

Random class

We can use Random class to generate some random numbers.
Random class is one of the classes used to generate random numbers. It can be used to generate different types of random numbers such as integers, floating point numbers, and vectors.

integer

We can use Random.Range to generate random integers or floating point numbers within a specified range. Here are two examples:

        // 随机生成一个浮点数
        float randomNumber = Random.Range(0f, 1f);
        Debug.Log("随机float: " + randomNumber);

        // 随机生成一个整数
        int randomInt = Random.Range(1, 10);
        Debug.Log("随机int: " + randomInt);

The running results are as follows:
Insert image description here

2D vector

Use Random.insideUnitCircle to obtain a random two-dimensional vector within a unit circle. This method is often used to generate random positions. The code looks like this:

        // 随机生成一个二维向量
        Vector2 randomVector2 = Random.insideUnitCircle;
        Debug.Log("随机Vector2: " + randomVector2);

The running results are as follows:
Insert image description here

3D vector

Use Random.insideUnitSphere to obtain a random three-dimensional vector within a unit sphere. This method is often used to generate random direction and motion effects.

        // 随机生成一个三维向量
        Vector3 randomVector3 = Random.insideUnitSphere;
        Debug.Log("随机Vector3: " + randomVector3);

The running results are as follows:
Insert image description here

seed

If someone has played Minecraft, they may know about seeds. When we play Minecraft, we all know that maps are randomly generated, but we also know that the same seed will generate the same map. Why is this? In fact, random number generation is based on seed calculation, and the same seed will generate the same random sequence.

Random number generation in Unity is also based on a pseudo-random number algorithm, that is, they are actually deterministic results calculated from a seed. This means that the same seed will always generate the same random sequence.

By default, the seed of the Random class is based on the system time, but you can also set the seed customarily using the Random.InitState method. This method accepts an integer as a parameter, which is used to initialize the seed of the random number generator.

    	public int seed = 12345;  // 自定义的种子值
        Random.InitState(seed);  // 设置种子
        // 根据自定义种子生成随机数
        float randomNum = Random.Range(0f, 1f);
        int randomInt = Random.Range(1, 10);
        Vector3 randomVector = Random.insideUnitSphere;

        Debug.Log("随机float: " + randomNum);
        Debug.Log("随机int: " + randomInt);
        Debug.Log("随机Vector: " + randomVector);

In the above example, we first define an integer variable seed to store a custom seed value. Then, we use Random.InitState(seed) to set the seed. Next, we use other methods of the Random class such as Random.Range and Random.insideUnitSphere to generate random numbers based on a custom seed.

Run it multiple times and you'll get the same result:
Insert image description here
by setting a custom seed, you can ensure that the same random sequence is generated with the same seed value. This is useful in situations where a specific random sequence needs to be reproduced. However, it should also be noted that this may lead to some problems, such as the predictability of random events or the repeatability of randomly generated object layouts. Therefore, make your choice based on your project needs.

Other articles

Unity realizes the effect of screen shaking after the character is attacked.
Unity realizes 2D game following the camera (smooth movement)

Guess you like

Origin blog.csdn.net/weixin_44499065/article/details/133393696