Skip to content

Instantly share code, notes, and snippets.

@alexzzzz
Last active December 31, 2015 18:19
Show Gist options
  • Select an option

  • Save alexzzzz/b9fa7f696482687f172e to your computer and use it in GitHub Desktop.

Select an option

Save alexzzzz/b9fa7f696482687f172e to your computer and use it in GitHub Desktop.
Generated by Code Connect's Gistify
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