Skip to content

Instantly share code, notes, and snippets.

@CSaratakij
Created February 27, 2020 10:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CSaratakij/da834807d23db86d67b270765c315685 to your computer and use it in GitHub Desktop.
Save CSaratakij/da834807d23db86d67b270765c315685 to your computer and use it in GitHub Desktop.
Shuffle sample (Generic)
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