using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class TimeRandom : MonoBehaviour { public Text txtNum; public Text[] txtR; int[] CNum = new int[4]; float fStartTime=0.0f; int iCount; // Use this for initialization void Start () { fStartTime = Time.time; iCount = 1; } // Update is called once per frame void Update () { //int iNum; int i, j; if ((Time.time - fStartTime) >= iCount) { //iNum = Random.Range (0, 10); //txtNum.text = iNum.ToString(); genRnd (0, 9, 4); for (i = 0; i < 4; i++) txtR [i].text = CNum [i].ToString(); iCount++; } } //產生不重複隨機亂數 public void genRnd(int min, int max, int takeOut) { int total = max - min + 1; int remain = total; int[] t = new int[total]; int i = 0; int j = 0; for (i = 0; i < total; i++) { t[i] = min + i; } for (i = 0; i < takeOut; i++) { int temp = (int)Random.Range(0, remain); Debug.Log(temp.ToString() + " " + t[temp].ToString()); //int temp = Mathf.Floor(Random.Range(0,1)*remain) + 1; CNum[i] = t[temp]; //this["data" + i] = this["t" + temp]; for (j = temp; j < (remain - 1); j++) { t[j] = t[j + 1]; } remain--; } } }