/// <summary> Picks randomly an item from the collection. </summary>
        /// <typeparam name="T"> The type of items in the collection. </typeparam>
        /// <param name="items"> The collection of items to pick from. </param>
        /// <param name="randomisationStep"> The current randomisation step. </param>
        /// <returns></returns>
        public T GetRandom <T>(IEnumerable <T> items, ERandomisationStep randomisationStep = ERandomisationStep.NotRelevant)
        {
            var itemType = typeof(T);

            if (items.IsEmpty())
            {
                if (itemType.IsEnum)
                {
                    var defaultEnumerationItem = itemType.GetEnumerationItems <T>().FirstOrDefault(item => item.ToString() == EWord.None);

                    if (defaultEnumerationItem is null)
                    {
                        throw new Exception(ECoreLogMessage.EnumerationHasNoDefaultItem.Format(itemType.ToStringLikeCode()));
                    }

                    return(defaultEnumerationItem);
                }

                if (itemType.IsValueType)
                {
                    throw new Exception(ECoreLogMessage.NothingToSelectFrom);
                }

                if (itemType.IsClass)
                {
                    return(default);
예제 #2
0
        protected override T GetRandomCore <T>(IEnumerable <T> items, ERandomisationStep randomisationStep)
        {
            if (typeof(T) == typeof(EBranch) && randomisationStep == ERandomisationStep.MainBranchWhenSelectingByCategories)
            {
                var branches  = items.OfType <EBranch>();
                var branchSet = new BranchSet(branches);

                AddIfMissing(branchSet, branches);

                var mainBranchOccurrences = _mainBranchOccurrences[branchSet];
                var selectedBranch        = EBranch.None;

                if (mainBranchOccurrences.Values.AllEqual())
                {
                    selectedBranch = base.GetRandomCore(items, randomisationStep).CastTo <EBranch>();
                }
                else
                {
                    var mostOccurences            = mainBranchOccurrences.Max(keyValuePair => keyValuePair.Value);
                    var leastOccurrences          = mainBranchOccurrences.Min(keyValuePair => keyValuePair.Value);
                    var leastOccurredMainBranches = mainBranchOccurrences.GetKeyWhereValue(occurrenceCount => occurrenceCount == leastOccurrences);

                    IncreaseMainBranchWeights(branchSet, leastOccurredMainBranches, mostOccurences - leastOccurrences);

                    selectedBranch = GetRandomMainBranch(branchSet, branches);

                    ResetMainBranchWeights(branchSet, branches);
                }

                mainBranchOccurrences[selectedBranch] += EInteger.Number.One;

                return(selectedBranch.CastTo <T>());
            }
            else
            {
                return(base.GetRandomCore(items, randomisationStep));
            }
        }
 protected virtual T GetRandomCore <T>(IEnumerable <T> items, ERandomisationStep randomisationStep)
 {
     return(items.ToList()[_generator.Next(items.Count())]);
 }