컴퓨터 프로그래밍2010. 6. 6. 22:43
public static class RandomSelector
{
    static Random random = new Random();
    public static T Random<T>(this IEnumerable<T> enumerable)
    {
        int c = enumerable.Count();
        int i = random.Next(c);
        return enumerable.Skip(i).First();
    }
}

프로젝트에 새 파일이나 기존소스에 추가해줍니다.

사용방법
int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int b = a.Random();

List<int> a = new List<int>();
....
....
int b = a.Random();

Posted by orange code