首页 > 其他 > 详细

获取序列的随机元素

时间:2019-12-02 17:23:43      阅读:76      评论:0      收藏:0      [点我收藏+]
            List<int> source = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            Random r = new Random();
            for (int i = 0; i < 100; i++)
            {
                Console.WriteLine($"Random: {source.RandomElement(r)}");
            }

        /// <summary>
        /// 从序列中选择随机元素 
        /// O(n)序列中每个元素被选择的机会是均等的,而且我们只迭代了一次
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <param name="random">同一个Random</param>
        /// <returns></returns>
        public static T RandomElement<T>(this IEnumerable<T> source, Random random)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            if (random == null)
            {
                throw new ArgumentNullException("random");
            }
            ICollection collection = source as ICollection;
            if (collection != null)
            {
                int count = collection.Count;
                if (count == 0)
                {
                    throw new InvalidOperationException("Sequence was empty.");
                }
                int indx = random.Next(count);
                return source.ElementAt(indx);
            }
            using (IEnumerator<T> iter = source.GetEnumerator())
            {
                if (!iter.MoveNext())
                {
                    throw new InvalidOperationException("Sequence was empty.");
                }
                int cntSoFar = 1;
                T current = iter.Current;
                while (iter.MoveNext())
                {
                    cntSoFar++;
                    if (random.Next(cntSoFar) == 0)
                    {
                        current = iter.Current;
                    }
                }
                return current;
            }
        }

技术分享图片

获取序列的随机元素

原文:https://www.cnblogs.com/wesson2019-blog/p/11971583.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!