public void Main()
{
//Fisher-Yates Shuffle algorithm
int[] ar = {1,2,3,4,5,6,7,8,9};
string[] st = "The dead road in the silent city" +
"roars and groans as he moves swiftly".Split();
Shuffle(ar);
Shuffle(st);
foreach(var i in ar)
Console.Write(i + " ");
Console.WriteLine();
foreach(var i in st)
Console.Write(i + " ");
}
public void Shuffle<T>(T[] array)
{
Random r = new Random();
int temp;
for(int i=array.Length; i>1; i--)
{
temp = r.Next(i);
T tmp = array[temp];
array[temp] = array[i-1];
array[i-1] = tmp;
}
}
Sample Output:
7 3 6 8 2 9 5 1 4
city silent road groans as swiftly dead he moves in roars and The the
No comments:
Post a Comment