Last active
December 31, 2015 18:19
-
-
Save alexzzzz/b9fa7f696482687f172e to your computer and use it in GitHub Desktop.
Generated by Code Connect's Gistify
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; | |
| using System.Linq; | |
| using Random = UnityEngine.Random; | |
| public class UniqueRandom | |
| { | |
| private readonly int[] numbers; | |
| private int index; | |
| public UniqueRandom(int min, int max) | |
| { | |
| numbers = Enumerable.Range(min, max).ToArray(); | |
| index = numbers.Length; | |
| } | |
| public int GetNext() | |
| { | |
| if (index == numbers.Length) | |
| { | |
| Shuffle(); | |
| index = 0; | |
| } | |
| return numbers[index++]; | |
| } | |
| private void Shuffle() => Array.Sort(numbers, (a, b) => Random.Range(0, 3) - 1); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment