using UnityEngine;
using System.Collections;

//打亂亂數
public class UpsetRandom : MonoBehaviour
{
	public static void Shuffle(T[] Source)
	{
		if (Source == null) return;
		
		int len = Source.Length;

		int r;
		
		//暫存用
		T tmp;
		
		for (int i = 0; i < len - 1; i++)
		{
			//取亂數,範圍包含最小值,不包含最大值
			r = Random.Range(i, len);

			//如果一樣則重取            
			if (i == r) continue;
			
			//取亂數後的索引與原來的交換
			tmp = Source[i];
			Source[i] = Source[r];
			Source[r] = tmp;
		}
	}
}
  1. 將陣列傳入
  2. 原理就是把陣列的索引打亂

 

歡迎轉載,但務必註明出處!!

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 arkai 的頭像
    arkai

    Game & Web Design

    arkai 發表在 痞客邦 留言(0) 人氣()