CSharp · Console
using System;
using System.Collections.Generic;
public static class Suffler
{
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 class Program
{
public static void Main()
{
var rnd = new Random();
//create array
int[] a = new int[5];
int[,] counts = new int[5,5];
for (int i = 0; i < a.Length; ++i) {
a[i] = i;
for (int j = 0; j < a.Length; ++j)
counts[i, j] = 0;
}
double trials = 1E8;
for (int t = 0; t < trials; ++t) {
Suffler.Shuffle(a, rnd);
for (int j = 0; j < a.Length; ++j)
counts[a[j], j]++;
}
for (int i = 0; i < a.Length; ++i) {
for (int j = 0; j < a.Length; ++j)
Console.Write("{0}\t", counts[i, j]/trials);
Console.WriteLine();
}
}
}