随笔 - 60  文章 - 11 评论 - 245 trackbacks - 2

using System;
using System.Collections.Generic;
using System.Linq;

public static class EnumerableExtensions {

    public static IEnumerable<TSource> Shuffle<TSource>(this IEnumerable<TSource> source) {
        List<TSource> list = source.ToList();
        Random random = new Random();

        for (int i = list.Count - 1; i >= 0; i--) {
            int r = random.Next(i + 1);
            yield return list[r];
            list[r] = list[i];
        }
    }

}
posted on 2010-11-10 09:54 Ants 阅读(61) 评论(0) 编辑 收藏