예제 #1
0
        /// <summary>
        /// 从指定的多个值中,随机挑选一个返回。
        /// </summary>
        /// <typeparam name="T">要随机挑选的值的类型。</typeparam>
        /// <param name="items">要随机挑选的值。</param>
        /// <returns>随机挑选的值。</returns>
        /// <exception cref="ArgumentNullException"><paramref name="items"/> 为 <c>null</c>。</exception>
        /// <exception cref="ArgumentException"><paramref name="items"/> 为空数组。</exception>
        public static T Choose <T>(params T[] items)
        {
            CommonExceptions.CheckCollectionEmpty(items, nameof(items));
            Contract.EndContractBlock();
            if (items.Length == 1)
            {
                return(items[0]);
            }
            var index = Random.Next(items.Length);

            return(items[index]);
        }