Skip to content

Instantly share code, notes, and snippets.

@wowbroforce
Created June 8, 2015 11:38
Show Gist options
  • Save wowbroforce/6ea4606a7b0f8c954ee1 to your computer and use it in GitHub Desktop.
Save wowbroforce/6ea4606a7b0f8c954ee1 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Linq;
using System.Collections.Generic;
public static class LinqExtensions {
public static T Random<T>(this IEnumerable<T> list) {
if (list != null && list.Any ())
return list.ElementAt(UnityEngine.Random.Range(0, list.Count()));
return default(T);
}
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source) {
var list = source.ToList();
list.Shuffle();
return list;
}
public static void Shuffle<T>(this IList<T> list) {
int count = list.Count;
while (count > 1) {
int i = UnityEngine.Random.Range(0, count - 1);
T temp = list[count];
list[count] = list[i];
list[i] = temp;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment