Random.state Random.value

Random.state

Other Versions

Leave feedback

public static Random.State state;

Description

Gets/Sets the full internal state of the random number generator.

This property is usually used to save and restore a previously saved state of the random number generator (RNG). The RNG state can also be initialized with a seed using the Random.InitState function, but the effect is quite different, as shown in the following example.

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
    void Start()
    {
        Random.InitState(255);

        PrintRandom("Step 1");
        PrintRandom("Step 2");

        Random.State oldState = Random.state;

        PrintRandom("Step 3");
        PrintRandom("Step 4");

        Random.state = oldState;

        PrintRandom("Step 5");
        PrintRandom("Step 6");

        Random.InitState(255);

        PrintRandom("Step 7");
        PrintRandom("Step 8");
    }

    void PrintRandom(string label)
    {
        Debug.Log(string.Format("{0} - RandomValue {1}", label, Random.Range(1, 100)));
    }
}

After running this script we will observe that the random values from step 5 and 6 will be equal to those from step 3 and 4 because the internal state of the RNG was restored by using Random.state property. Also, the random values from step 7 and 8 will be equal to the ones from step 1 and 2 because Random.InitStatereinitializes the state of the RNG with the initial seed.

Random.value

Other Versions

Leave feedback

public static float value;

Description

Returns a random number between 0.0 [inclusive] and 1.0 [inclusive] (Read Only).

Both 0.0 and 1.0 may be returned by this property. This behaviour is different to that of many other random number generators which return a value less than but never exactly equal to 1.0.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值