var list = new List<T> ();fillList (list);var randomizedList = new List<T> ();var rnd = new Random ();while (list.Count != 0){var index = rnd.Next (0, list.Count);randomizedList.Add (list [index]);list.RemoveAt (index);}
public static List<T> Randomize<T>(List<T> list){List<T> randomizedList = new List<T>();Random rnd = new Random();while (list.Count > 0){int index = rnd.Next(0, list.Count); //pick a random item from the master listrandomizedList.Add(list[index]); //place it at the end of the randomized listlist.RemoveAt(index);}return randomizedList;}
public byte[] Shuffle(byte[] array, int start, int count){int n = array.Length - start;byte[] shuffled = new byte[count];for(int i = 0; i < count; i++, start++){int k = UniformRandomGenerator.Next(n--) + start;shuffled[i] = array[k];array[k] = array[start];array[start] = shuffled[i];}return shuffled;}
public static class EnumerableExtension{private static Random globalRng = new Random();
[ThreadStatic]private static Random _rng;
private static Random rng{get{if (_rng == null){int seed;lock (globalRng){seed = globalRng.Next();}_rng = new Random(seed);}return _rng;}}
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> items){return items.OrderBy (i => rng.Next());}}
public static void Shuffle<T>(this IList<T> list, Random rnd){for(var i=list.Count; i > 0; i--)list.Swap(0, rnd.Next(0, i));}
public static void Swap<T>(this IList<T> list, int i, int j){var temp = list[i];list[i] = list[j];list[j] = temp;}
public static class IEnumerableExtensions{
public static IEnumerable<t> Randomize<t>(this IEnumerable<t> target){Random r = new Random();
return target.OrderBy(x=>(r.Next()));}}
您可以通过执行以下操作来使用它
// use this on any collection that implements IEnumerable!// List, Array, HashSet, Collection, etc
List<string> myList = new List<string> { "hello", "random", "world", "foo", "bar", "bat", "baz" };
foreach (string s in myList.Randomize()){Console.WriteLine(s);}
public static IList<T> NextList<T>(this Random r, IEnumerable<T> source){var list = new List<T>();foreach (var item in source){var i = r.Next(list.Count + 1);if (i == list.Count){list.Add(item);}else{var temp = list[i];list[i] = item;list.Add(temp);}}return list;}
private static Random rng = new Random();
/// <summary>/// Returns a new list where the elements are randomly shuffled./// Based on the Fisher-Yates shuffle, which has O(n) complexity./// </summary>public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> list) {var source = list.ToList();int n = source.Count;var shuffled = new List<T>(n);shuffled.AddRange(source);while (n > 1) {n--;int k = rng.Next(n + 1);T value = shuffled[k];shuffled[k] = shuffled[n];shuffled[n] = value;}return shuffled;}
List<T> OriginalList = new List<T>();List<T> TempList = new List<T>();Random random = new Random();int length = OriginalList.Count;int TempIndex = 0;
while (length > 0) {TempIndex = random.Next(0, length); // get random value between 0 and original lengthTempList.Add(OriginalList[TempIndex]); // add to temp listOriginalList.RemoveAt(TempIndex); // remove from original listlength = OriginalList.Count; // get new list <T> length.}
OriginalList = new List<T>();OriginalList = TempList; // copy all items from temp list to original list.
public static IList<T> TakeRandom<T>(this IEnumerable<T> collection, int count, Random random) => shuffle(collection, count, random);public static IList<T> Shuffle<T>(this IEnumerable<T> collection, Random random) => shuffle(collection, null, random);private static IList<T> shuffle<T>(IEnumerable<T> collection, int? take, Random random){var a = collection.ToArray();var n = a.Length;if (take <= 0 || take > n) throw new ArgumentException("Invalid number of elements to return.");var end = take ?? n;for (int i = 0; i < end; i++){var j = random.Next(i, n);(a[i], a[j]) = (a[j], a[i]);}
if (take.HasValue) return new ArraySegment<T>(a, 0, take.Value);return a;}
public static void Shuffle<T>(this IList<T> list, Random rnd){for (var i = list.Count-1; i > 0; i--){var randomIndex = rnd.Next(i + 1); //maxValue (i + 1) is EXCLUSIVElist.Swap(i, randomIndex);}}
public static void Swap<T>(this IList<T> list, int indexA, int indexB){var temp = list[indexA];list[indexA] = list[indexB];list[indexB] = temp;}
public class RandomIntComparer : IComparer<int>{private readonly Random _random = new Random();
public int Compare(int x, int y){return _random.Next(-1, 2);}}
private static readonly Random random = new Random();
public static void Shuffle<T>(this IList<T> list){int n = list.Count;while (n > 1){n--;int k = random.Next(n + 1);(list[k], list[n]) = (list[n], list[k]);}}
public static class ListExtensions{public static void Shuffle<T>(this IList<T> list){if (list == null) throw new ArgumentNullException(nameof(list));int n = list.Count;while (n > 1){n--;int k = ThreadSafeRandom.Next(n + 1);(list[n], list[k]) = (list[k], list[n]);}}}
internal class ThreadSafeRandom{private static readonly Random _global = new Random();private static readonly ThreadLocal<Random> _local = new ThreadLocal<Random>(() =>{int seed;lock (_global){seed = _global.Next();}return new Random(seed);});
public static int Next(int maxValue){return _local.Value.Next(maxValue);}}