unity中利用C#產生隨機不相同的一組數
阿新 • • 發佈:2019-02-15
using UnityEngine; using System.Collections; using System.Collections.Generic; public class game : MonoBehaviour { //List列表存放生成的隨機數 public List <int> mNumberList = new List<int>(); //生成的隨機數 int rangeNubers; // Use this for initialization void Start () { //產生數字存放到List裡 checkNumber(); } // Update is called once per frame void Update () { } //產生數字存放到List裡 public void checkNumber(){ while(mNumberList.Count <=4){ rangeNumber(); if(!mNumberList.Contains(rangeNubers)){ mNumberList.Add(rangeNubers); } else{ continue; } } } //產生隨機數 public void rangeNumber(){ rangeNubers =Random.Range(0,5); } }