["..E\$ ","A(CAssXZCE|F7566666885}XBaCC*BsBCE`","Shuffle."," We can shuffle an array, randomly reordering all elements, with results that are mathematically correct. Some solutions exist but do not give high-quality random results.","Example."," Here we see an approach to shuffling a string array that is not the classic, optimized Fisher-Yates shuffle. But this approach is mathematically random. It will not cause strange biases in your code. ","This is true because it performs all the operations together, rather than one after another.","Fisher-Yates Shuffle ","fisher-yates-shuffle"," .NET 4.6\n\n","C# program that uses random shuffle algorithm","\n\nusing System;\nusing System.Collections.Generic;\nusing System.Linq;\n\nclass Program\n{\n static ","Random"," _random = new Random();\n\n static string[] ","RandomizeStrings","(string[] arr)\n {\n List<KeyValuePair<int, string>> list =\n new List<KeyValuePair<int, string>>();","\n // Add all strings from array.\n // ... Add new random int each time.\n ","foreach (string s in arr)\n {\n list.Add(new KeyValuePair<int, string>(_random.Next(), s));\n }","\n // Sort the list by the random number.\n ","var sorted = from item in list\n orderby item.Key\n select item;","\n // Allocate new string array.\n ","string[] result = new string[arr.Length];","\n // Copy values to array.\n ","int index = 0;\n foreach (KeyValuePair<int, string> pair in sorted)\n {\n result[index] = pair.Value;\n index++;\n }","\n // Return copied array.\n ","return result;\n }\n\n static void Main()\n {\n string[] arr = new string[]\n {\n ","\"cat\"",",\n ","\"animal\"",",\n ","\"abacus\"",",\n ","\"framework\"","\n };\n string[] shuffle = ","RandomizeStrings","(arr);\n foreach (string s in shuffle)\n {\n Console.WriteLine(s);\n }\n }\n}\n\n","\n\nabacus\nframework\ncat\nanimal","The method here"," uses the KeyValuePair<T, V> data structure that is included in System.Collections.Generic. It allocates another array containing the string[] elements and pairs them with a random number. Finally, it sorts. ","KeyValuePair ","keyvaluepair","This method"," is equally random as generating a random set of integers one-by-one. If you only randomize one element at a time, and then randomize the rest separately, you will get biases. ","We randomize the entire array all at once, which will result in consistently random results.","Note 2: ","I decided to use this approach instead of the optimized algorithms because this code wasn't on a hot path in my program.","Random numbers."," It stores a Random number generator as a static field, which means you can call the RandomizeStrings method repeatedly and will get good results. More information on Random is available. ","Random ","random","Discussion."," This article previously showed a naive method that used OrderBy in LINQ, but was not mathematically sound. The current method is mathematically sound, as noted in Wikipedia. See \"Comparison with other shuffling algorithms.\" ","orderby ","orderby","Info: ","Wikipedia notes that this approach is sometimes faster in high-level languages. It is unlikely to be faster in the C# language.","Fisher-Yates shuffle: Wikipedia ","https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle","Summary."," We used a mathematically sound approach for shuffling an array. This method is not optimally fast, but for my use this wasn't important. If you need performance, use an implementation of Fisher-Yates."]