随机指定范围指定数量的随机数
using System.Collections.Generic;
using UnityEngine;
public class RandomNumber : MonoBehaviour
{
public int beginNum, endNum, getCount;
void Awake()
{
beginNum = 1;
endNum = 100;
getCount = 10;
}
///
/// 调用
///
private void Start()
{
// 从1至100中随机10个不相同的数
List arrayList = GetRandomNumberList(beginNum, endNum, getCount);
for (int i = 0; i < arrayList.Count; i++)
{
Debug.Log("_____________________________________ " + arrayList[i]);
}
}
///
/// 随机指定范围指定数量
///
/// 起始数
/// 结束数
/// 随机的数量
///
private List GetRandomNumberList(int beginNum, int endNum, int getCount)
{
List resultArray = new List();
List originalArray = new List();
for (int i = beginNum; i <= endNum; i++)
{
originalArray.Add(i);
}
int randomCount = originalArray.Count;
int randomIndex = 0, count = randomCount, temp = 0;
for (int i = 0; i < getCount; i++)
{
randomIndex = UnityEngine.Random.Range(0, count);
resultArray.Add(originalArray[randomIndex]);
if (randomIndex != count - 1)
{
temp = originalArray[randomIndex];
originalArray[randomIndex] = originalArray[count - 1];
originalArray[count - 1] = temp;
}
count--;
}
return resultArray;
}
}