Skip to content

Instantly share code, notes, and snippets.

@paulhayes
Last active November 30, 2015 17:57
Show Gist options
  • Save paulhayes/6a739dbd5853d6f617f6 to your computer and use it in GitHub Desktop.
Save paulhayes/6a739dbd5853d6f617f6 to your computer and use it in GitHub Desktop.
Written with Unity3d in mind, this adds a few simple seedable Randomization extention methods to Arrays and Lists.
using System.Collections.Generic;
static class RandArrayExt
{
public static System.Random random = new System.Random();
public static T[] Shuffle<T> (this T[] array)
{
if (array == null)
return array;
System.Random rand = new System.Random();
int l = (array.Length - 1);
for (int i=0; i<l; i++) {
T tmp = array [i];
int r = rand.Next (i, array.Length);
array [i] = array [r];
array [r] = tmp;
}
return array;
}
public static IList<T> Shuffle<T> (this IList<T> array)
{
if (array == null)
return null;
System.Random rand = new System.Random();
int l = array.Count;
for (int i=0; i<(l-1); i++) {
T tmp = array [i];
int r = rand.Next (i, l);
array [i] = array [r];
array [r] = tmp;
}
return array;
}
public static T PickRandom<T> (this T[] array)
{
int i = random.Next (0, array.Length);
return array [i];
}
public static T PickRandom<T>(this List<T> array)
{
int i = random.Next (0, array.Count);
return array [i];
}
public static T Shuffled<T>(this T[] array, int seed, int index)
{
int shuffleRange = array.Length;
int output = index % shuffleRange;
int cycleIndex = index / shuffleRange;
while (output < shuffleRange)
{
output = (output + cycleIndex + seed) % shuffleRange;
shuffleRange--;
}
return array[output];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment