Unity random number and a random seed

Random number used in almost all aspects of game development, for example, randomly generated maps, mazes, monsters and other properties, in Unity, the use of random numbers is very easy:

 1         //
 2         // 摘要:
 3         //     Return a random integer number between min [inclusive] and max [exclusive] (Read
 4         //     Only).
 5         //
 6         // 参数:
 7         //   min:
 8         //
 9         //   max:
10         public static int Range(int min, int max);
 1         //
 2         // 摘要:
 3         //     Return a random float number between min [inclusive] and max [inclusive] (Read
 4         //     Only).
 5         //
 6         // 参数:
 7         //   min:
 8         //
 9         //   max:
10         [FreeFunction]
11         public static float Range(float min, float max);
1         //
2         // 摘要:
3         //     Returns a random number between 0.0 [inclusive] and 1.0 [inclusive] (Read Only).
4         public static float value { get; }

Use these three completely enough under normal circumstances, attention integer random left and right to open and close. Of course, you can also use the method System.Random randomly, we can construct an extension method similar to the Unity of:

 1     static public int Range(this System.Random random, int min, int max)
 2     {
 3         return random.Next(min, max);
 4     }
 5 
 6     static public float Range(this System.Random random, float min, float max)
 7     {
 8         var r = random.NextDouble();
 9         return (float)(r * (max - min) + min);
10     }

It is noteworthy that, System.Random need to be instantiated to random, while UnityEngine.Random be used directly.

 

But very often, we need in addition to the random number, there may be a demand to retain the last result of random, in other words, from a certain moment, we hope that every time a random and the same result as last time, this time in respect of random seed played.

For example, when the player needs to re-enter the time he had come out before a random maze map for secondary creation, and for example, we are in the development process, there have been some random units Bug, but before the next if they can not produce random results, then will be very troublesome situation there, so it may never have a difficult investigation of potential Bug has been in the development process and difficult to reproduce again.

Therefore, it is strongly recommended, as long as doing relatively complex stochastic behavior, we use the best random seed to perform random .

Of course, you say I have all the result of random data sequence preservation locally, that's no problem, but compared to random seed only need to save a integer data, it is obvious which way is more preferable. This also can greatly reduce the saved game data capacity.

 

Having said that for a long time, what is random seed it?

As the name suggests, a seed corresponds to a result, the corresponding random seed is a unique random results.

 1         //
 2         // 摘要:
 3         //     Initializes the random number generator state with a seed.
 4         //
 5         // 参数:
 6         //   seed:
 7         //     Seed used to initialize the random number generator.
 8         [NativeMethod("SetSeed")]
 9         [StaticAccessor("GetScriptingRand()", StaticAccessorType.Dot)]
10         public static void InitState(int seed);

The above method, the parameter passed in random seed is the seed, if at the beginning of the script calling this method, only when the last random seed and the seeds are not the same, a different random order random results, otherwise random results are always the same.

Note that, in this case refers to a random result of all the random outcome is a random number table, it changes from essentially the entire result UnityEngine.Random all randomized class method implementation, including the three most enumerated beginning either.

What follows is a test very easy to understand:

 1 using UnityEngine;
 2 
 3 public class RanTest : MonoBehaviour
 4 {
 5     public bool bDebug;
 6     //System.Random random;
 7     void Start()
 8     {
 9         //random = new System.Random((int)System.DateTime.Now.Ticks);
10         //string s = "";
11         //for(int i = 0; i < 233; i++)
12         //{
13         //    s += random.Range(0, 10) + ",";
14         //}
15         //Debug.Log(s);
16 
17         int seed = (int)System.DateTime.Now.Ticks;
18         if (bDebug)
19         {
20             seed = PlayerPrefs.GetInt("Seed");   
21         }
22         else
23         {
24             PlayerPrefs.SetInt("Seed", (int)System.DateTime.Now.Ticks);
25         }
26         Random.InitState(seed);
27         string s = "";
28         for (int i = 0; i < 32; i++)
29         {
30             s += Random.Range(0, 10) + ",";
31         }
32         Debug.Log(s);
33     }
34 }

For example, I opened up a Debug mode, if checked, random seed data is read from the last saved randomly out of the result is always the same, because I did not make any changes to the data stored seeds.

The results are as follows:

 

We found that each of the random numbers are the same, because they all come from the same random seed, no matter how many times after randomly, the result is the random number sequence corresponding to the result of this seed has been fixed the computer, unless seed change, otherwise, the results will not change randomly.

When I closed Debug mode, normal random seed will not be the same all the time, used here to ensure the System.DateTime.Now.Ticks and get the last seed never the same integer, and so on can be used guid.

The results of each local backup once in the last recorded random seed, so that at any time can be reproduced on a random, only Debug can easily check:

 

 For example, when I found other Bug randomly generated in the third, so I only start in Debug mode analysis repeated several times after the reproduction of the hidden Bug Modify the end and then back to the normal mode generates a new random number just fine.

 

Furthermore, we can also use System.Random class constructor to achieve the same random effects, a constructor parameter having a random seed, a no, the principle is the same as above:

 1 public Random(); 2 public Random(int Seed); 

This time change is random method System.Random class, rather than the random method of UnityEngine.Random.

So a good decision to start random type used throughout the development process can not be ignored, it is recommended that either all in the Unity, or to the entire System, so adjust them with more natural with less handy.

Guess you like

Origin www.cnblogs.com/koshio0219/p/12514825.html