andrew4582

RandomizeStrings

Mar 14th, 2011
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.95 KB | None | 0 0
  1. static Random _random = new Random();
  2.         public static string[] RandomizeStrings(string[] arr) {
  3.  
  4.             List<KeyValuePair<int,string>> list = new List<KeyValuePair<int,string>>();
  5.             // Add all strings from array
  6.             // Add new random int each time
  7.             foreach(string s in arr) {
  8.                 list.Add(new KeyValuePair<int,string>(_random.Next(),s));
  9.             }
  10.             // Sort the list by the random number
  11.             var sorted = from item in list
  12.                          orderby item.Key
  13.                          select item;
  14.             // Allocate new string array
  15.             string[] result = new string[arr.Length];
  16.             // Copy values to array
  17.             int index = 0;
  18.             foreach(KeyValuePair<int,string> pair in sorted) {
  19.                 result[index] = pair.Value;
  20.                 index++;
  21.             }
  22.             // Return copied array
  23.             return result;
  24.         }
Advertisement
Add Comment
Please, Sign In to add comment