返回介绍

Random.seed 种子

发布于 2019-12-18 15:38:22 字数 2941 浏览 1003 评论 0 收藏 0

JavaScript => public static var seed: int;
C# => public static int seed;

Description 描述

Sets the seed for the random number generator.

给随机数发生器设置种子。

The random number generator is not truly random but produces numbers in a preset sequence (the values in the sequence “jump” around the range in such a way that they appear random for most purposes).

随机数发生器不是真正的随机(伪随机数),它产生的随机数是预先设定的顺序(对于大多数情况而言,这个随机的值是在一个序列范围内跳动)。

The point in the sequence where a particular run of pseudo-random values begins is selected using an integer called the seed value. The seed is normally set from some arbitrary value like the system clock before the random number functions are used. This prevents the same run of values from occurring each time a game is played and thus avoids predictable gameplay. However, it is sometimes useful to produce the same run of pseudo-random values on demand by setting the seed yourself.

在序列中运行的随机的点 开始随机时是通过一个叫做种子的值被从序列中挑选出来的。在random方法被调用之前,种子的设置通常来自一些随意的值比如系统时间。这可以避免在游戏运行时出现相同的随机数,从而避免可预见的游戏。然而,通过相同种子产生的伪随机数也可以满足某些需求的。

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.

你可以设置你自己的种子,例如,当你用程序生成了一个游戏关卡。您可以使用随机选择的元素,使现场看起来随意和自然,但之前产生种子设定为预设值。

JavaScript:

private var noiseValues: float[];
 
function Start () {
	// If you comment out the following line, the sequence of random
	// values will be different each time you run. However, the sequence
	// will always be the same for any particular seed value.
	Random.seed = 42;
 
	noiseValues = new float[10];
 
	for (var i = 0; i < noiseValues.Length; i++) {
		noiseValues[i] = Random.value;
		print(noiseValues[i]);
	}
}

C#:

using UnityEngine;
using System.Collections;
 
public class ExampleClass : MonoBehaviour {
    private float[] noiseValues;
    void Start() {
        Random.seed = 42;
        noiseValues = new float[10];
        int i = 0;
        while (i < noiseValues.Length) {
            noiseValues[i] = Random.value;
            print(noiseValues[i]);
            i++;
        }
    }
}

Random

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文