Exemple #1
0
        /// <summary>
        /// Generates a randomized list from given enumerable.
        /// </summary>
        /// <typeparam name="T">Type of items in the list</typeparam>
        /// <param name="items">items</param>
        public static List <T> GenerateRandomizedList <T>(IEnumerable <T> items)
        {
            CheckValue.NotNull(items, nameof(items));

            var currentList = new List <T>(items);
            var randomList  = new List <T>();

            while (currentList.Any())
            {
                var randomIndex = RandomHelper.GetRandom(0, currentList.Count);
                randomList.Add(currentList[randomIndex]);
                currentList.RemoveAt(randomIndex);
            }

            return(randomList);
        }
Exemple #2
0
        /// <summary>
        /// Gets random item from the given list.
        /// </summary>
        /// <typeparam name="T">Type of the objects</typeparam>
        /// <param name="list">List of object to select a random one</param>
        public static T GetRandomOfList <T>(IList <T> list)
        {
            CheckValue.NotNullOrEmpty(list, nameof(list));

            return(list[GetRandom(0, list.Count)]);
        }
Exemple #3
0
        /// <summary>
        /// Gets random of given objects.
        /// </summary>
        /// <typeparam name="T">Type of the objects</typeparam>
        /// <param name="objs">List of object to select a random one</param>
        public static T GetRandomOf <T>(params T[] objs)
        {
            CheckValue.NotNullOrEmpty(objs, nameof(objs));

            return(objs[GetRandom(0, objs.Length)]);
        }