Advertisement
Guest User

Untitled

a guest
Jul 5th, 2015
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. public static class LinqExtensions {
  6. public static T Random<T>(this IEnumerable<T> list) {
  7. if (list != null && list.Any ())
  8. return list.ElementAt(UnityEngine.Random.Range(0, list.Count()));
  9. return default(T);
  10. }
  11.  
  12. public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source) {
  13. var list = source.ToList();
  14. list.Shuffle();
  15. return list;
  16. }
  17.  
  18. public static void Shuffle<T>(this IList<T> list) {
  19. int count = list.Count;
  20. while (count > 1) {
  21. int i = UnityEngine.Random.Range(0, count - 1);
  22. T temp = list[count];
  23. list[count] = list[i];
  24. list[i] = temp;
  25. }
  26. }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement