Created
February 27, 2020 10:47
-
-
Save CSaratakij/da834807d23db86d67b270765c315685 to your computer and use it in GitHub Desktop.
Shuffle sample (Generic)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class Utils : MonoBehaviour | |
{ | |
int[] numbers = new int[] { | |
1, 2, 3, 4, 5, 6, 7, 0, 0, 0, 0, 0, 0, 0 | |
}; | |
void Start() | |
{ | |
string before = ""; | |
foreach (int number in numbers) | |
{ | |
before += number + " "; | |
} | |
Debug.Log("Before : " + before); | |
string after = ""; | |
numbers = Shuffle(numbers); | |
foreach (int number in numbers) | |
{ | |
after += number + " "; | |
} | |
Debug.Log("After : " + after); | |
} | |
T[] Shuffle<T>(T[] collection) | |
{ | |
Random.InitState(Random.Range(0, 100)); | |
for (int i = 0; i < collection.Length; ++i) | |
{ | |
int swapIndice = Random.Range(i, collection.Length); | |
T temp = collection[i]; | |
collection[i] = collection[swapIndice]; | |
collection[swapIndice] = temp; | |
} | |
return collection; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment