Unity 同时生成几个不同的随机数

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_40229737/article/details/99547875
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CreateRandom : MonoBehaviour {
    //List列表存放生成的随机数
    public List<int> rangeList = new List<int>();
    int temp_nums;
    int count;
    // Use this for initialization
    void Start()
    {
       //每次运行程序产生的随机数数量
        count = 3;
        CreateNums();
    }

    public void CreateNums()
    {
        //产生数字存放到列表里
        SaveToList();
        //打印出生成的随机数
        for (int i = 0; i < rangeList.Count; i++)
        {
            Debug.LogError(rangeList[i]);
        }

    }
    void SaveToList()
    {
       
        while (rangeList.Count < count)
        {
            temp_nums = Random.Range(0, 10);
            #region 判断列表中是否有此次生成的随机数,没有则把temp_nums加入列表,否则重新生成            
            if (!rangeList.Contains(temp_nums))
            {
                rangeList.Add(temp_nums);
            }
            else
            {
                continue;
            }
            #endregion
        }
    }
  



}

猜你喜欢

转载自blog.csdn.net/qq_40229737/article/details/99547875