コード例 #1
0
        private static T PopFromHeap(IReadOnlyList <T> choices, ref double[] weights, ref double[] totalWeights, IRandomProvider random)
        {
            var gas = random.NextDouble() * totalWeights[1];
            var i   = 1;

            while (gas >= weights[i])
            {
                gas -= weights[i];
                i  <<= 1;

                if (gas >= totalWeights[i])
                {
                    gas -= totalWeights[i];
                    i   += 1;
                }
            }

            var weight = weights[i];
            var chosen = choices[i];

            weights[i] = 0;

            while (i > 0)
            {
                totalWeights[i] -= weight;
                i >>= 1;
            }

            return(chosen);
        }
コード例 #2
0
        public static T PickSingleItem(List <WeightedChoice <T> > weights, IRandomProvider randomProvider)
        {
            var totalWeight = WeightsAddUpToOne(weights);

            if (Math.Abs(1d - totalWeight) > Sensitivity)
            {
                throw new ArgumentException($"The weights for the items to choose do not add up to one. Total weight = {totalWeight}");
            }

            var randomValue = randomProvider.NextDouble();

            for (var i = 0; i < weights.Count; i++)
            {
                var currentWeight = weights[i];

                if (i == weights.Count - 1)
                {
                    return(currentWeight.Choice);                        // last item on the list so it must be chosen
                }
                randomValue -= currentWeight.Weight;
                if (randomValue < 0)
                {
                    return(currentWeight.Choice);
                }
            }

            throw new Exception("Failed to make a random choice");
        }
コード例 #3
0
        public static IEnumerable <T> Shuffle <T>(this IEnumerable <T> enumerable, IRandomProvider randomProvider)
        {
            if (enumerable is null)
            {
                throw new ArgumentException("Enumerable is null.", "enumerable");
            }

            if (randomProvider is null)
            {
                throw new ArgumentException("Random Provider is null", "randomProvider");
            }

            return(enumerable.OrderBy(x => randomProvider.NextDouble()).ToList());
        }
コード例 #4
0
ファイル: Utils.cs プロジェクト: zdimension/TheWordMachine
        public static int Choice(int[] a, float[] p, IRandomProvider rng)
        {
            var roll = rng.NextDouble();

            var cum = 0.0;

            for (var i = 0; i < a.Length; i++)
            {
                cum += p[i];

                if (roll < cum)
                {
                    return(a[i]);
                }
            }

            return(0);
        }
コード例 #5
0
        public Question GetNextQuestion()
        {
            RefillAddoptions();

            Question question = null;

            if (randomProvider.NextDouble() > 0.3)
            {
                question = GetAdoptedQuestion();
            }

            if (question is null)
            {
                question = GetFreshQuestion();
            }

            if (question is null)
            {
                question = GetRrandomQuestion();
            }

            return(question);
        }
コード例 #6
0
ファイル: Reservoir.cs プロジェクト: andrebellingan/YAABM
        public static List <int> ReservoirSampling(IEnumerable <double> weightStream, int k, IRandomProvider random)
        {
            var wSum = 0d;
            var r    = new List <int>(k);

            if (k == 0)
            {
                return(r);
            }

            using var enumerator = weightStream.GetEnumerator();
            for (var x = 0; x < k; x++)
            {
                enumerator.MoveNext();
                var currentWeight = enumerator.Current;
                r.Add(x);
                wSum += currentWeight;
            }

            var i = k;

            while (enumerator.MoveNext())
            {
                wSum += enumerator.Current;
                var p = enumerator.Current / wSum;
                var j = random.NextDouble();
                if (j <= p)
                {
                    var index = random.NextInt(0, k);
                    r[index] = i;
                }

                i++;
            }

            return(r);
        }
コード例 #7
0
 void bar()
 {
     ans = r.NextDouble();