public static float Range (float min, float max);

描述

返回介于 min [含] 与 max [含] 之间的随机浮点数(只读)。

请注意,max 包含在内。Random.Range(0.0f, 1.0f) 可能会返回 1.0 作为值。Random.Range 分布是均匀的。Range 是一个随机数生成器。

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { public GameObject prefab;

// Instantiate the Prefab somewhere between -10.0 and 10.0 on the x-z plane void Start() { Vector3 position = new Vector3(Random.Range(-10.0f, 10.0f), 0, Random.Range(-10.0f, 10.0f)); Instantiate(prefab, position, Quaternion.identity); } }

public static int Range (int min, int max);

描述

返回介于 min [含] 与 max [不含] 之间的随机整数(只读)。

请注意,max 不包含在内。Random.Range(0, 10) 可以返回介于 0 与 9 之间的值。如果 max 等于 /min/,则返回 /min/。Random.Range 分布是均匀的。Range 是一个随机数生成器。

using UnityEngine;
using UnityEngine.SceneManagement;

public class ExampleClass : MonoBehaviour { // Loads a random level from the level list

void Start() { SceneManager.LoadScene(Random.Range(0, SceneManager.sceneCount)); } }