Unity 每帧里随机不同的数据(真随机)

需求:项目里需要对集合里每个个体进行单独计算概率
设计:对集合进行遍历,随机个百分比在概率内就判定成功
知识:使用到了System.Random随机种子,UnityEngine.Random.Range(0f, 1f)

第一种System.Random随机种子

第二种UnityEngine.Random.Range(0f, 1f)

推荐使用第二种!!!,量大第一种性能会比第二种差很多

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestRandom : MonoBehaviour
{
    
    
    // Start is called before the first frame update
    public int count = 10;
   
    void Start()
    {
    
    
        ButtonRandom();
    }

    // Update is called once per frame
    void Update()
    {
    
    
        if(Input.GetKeyDown(KeyCode.W))
        {
    
    
            ButtonRandom();
        }
    }

    public void ButtonRandom()
    {
    
    
        for (int i = 0; i < count; i++)
        {
    
    
            System.Random random = new System.Random(i);
            float j = UnityEngine.Random.Range(0f, 1f);
            var randomRestul = random.Next(0, 100);
            //new NeoWhimsy.NormalBoll();
            Debug.Log("randomRestul:"+randomRestul + "\t" + j);
        }
    }
}


猜你喜欢

转载自blog.csdn.net/qq_41179365/article/details/119610399