seed | 用于初始化随机数生成器的种子。 |
使用种子初始化随机数生成器状态。
随机数生成器并非真正随机,而是采用预设序列生成数字(对大多数用途而言,序列中的值表现为以看似随机的方式围绕范围“跳动”)。
序列开始生成一串特定伪随机值的点选自一个称为种子 值的整数。在使用随机数函数之前,种子通常是通过某个任意值(如系统时钟)进行设置的。这可防止在每次玩游戏时出现相同的一串值,从而避免可预测的游戏玩法。但是,有时通过自己设置种子,按照需要生成相同的一串伪随机值会十分有用。
You might set your own seed, for example, when you generate a game level procedurally. You can use randomly-chosen elements to make the Scene look arbitrary and natural but set the seed to a preset value before generating. This will make sure that the same "random" pattern is produced each time the game is played. This can often be an effective way to reduce a game's storage requirements - you can generate as many levels as you like procedurally and store each one using nothing more than an integer seed value.
using UnityEngine; using System.Collections;
public class ExampleClass : MonoBehaviour { private float[] noiseValues; void Start() { Random.InitState(42); noiseValues = new float[10]; for (int i = 0; i < noiseValues.Length; i++) { noiseValues[i] = Random.value; Debug.Log(noiseValues[i]); } } }