您的位置:首页 > 移动开发 > Unity3D

unity3d随机取出多个不同数

2018-03-11 09:49 274 查看
using System.Collections.Generic;
using UnityEngine;

public class Test1 : MonoBehaviour
{
private void Start()
{
//随机从0-100中取50个不同的数
List<int> values = TitleData.RandomNum(50, 100);
foreach (var item in values)
{
Debug.Log("value=" + item);//50个打印出来的数,没有相同的
}

}

}

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

public static class TitleData
{
private static Random random = new Random();
public static List<int> RandomNum(int wantNum,int dataCount)
{
HashSet<int> values = new HashSet<int>();
List<int> list = new List<int>();
int n;
      while (values.Count < wantNum)
        {
            n = random.Next(0, dataCount + 1);

                if (values.Add(n))
                {
                    list.Add(n);
                }
        }
return list;
}

}
在Unity3d中,使用UnityEngine.Random和System.Random是不同的,这里使用的是System.Random
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C# unity3d
相关文章推荐