Пример #1
0
        /// <summary>
        /// 主键权重随机
        /// </summary>
        public static string MTRandomKey(List <KeyValuePair <string, int> > weights, MersenneTwisterRandom mtRandom)
        {
            List <string> resultKeys = MTRandomKey(weights, 1, mtRandom);

            if (resultKeys.Count() > 0)
            {
                return(resultKeys[0]);
            }

            return(null);
        }
Пример #2
0
        /// <summary>
        /// 索引权重随机
        /// </summary>
        public static int MTRandomIndex(List <int> weights, MersenneTwisterRandom mtRandom)
        {
            List <int> resultIndexes = MTRandomIndex(weights, 1, mtRandom);

            if (resultIndexes.Count() > 0)
            {
                return(resultIndexes[0]);
            }

            return(-1);
        }
Пример #3
0
        /// <summary>
        /// 索引权重随机
        /// </summary>
        public static List <int> MTRandomIndex(List <int> weights, int count, MersenneTwisterRandom mtRandom)
        {
            int weightCount = weights.Count();

            if (weightCount == 0 || count <= 0)
            {
                return(null);
            }

            int totalWeight = 0;

            for (int i = 0; i < weightCount; i++)
            {
                totalWeight += weights[i];
            }

            if (totalWeight <= 0)
            {
                return(null);
            }

            List <int>    resultIndexes = new List <int>(count);
            HashSet <int> resultTags    = new HashSet <int>();

            while (resultIndexes.Count < count)
            {
                int randomWeight  = mtRandom.Next(0, totalWeight);
                int currentWeight = 0;

                for (int i = 0; i < weightCount; i++)
                {
                    currentWeight += weights[i];

                    if (currentWeight > randomWeight)
                    {
                        if (!resultTags.Contains(i))
                        {
                            resultIndexes.Add(i);
                            resultTags.Add(i);
                        }

                        break;
                    }
                }
            }

            return(resultIndexes);
        }
Пример #4
0
        /// <summary>
        /// 随机从 [0,max) 中取出不重复的 num 个整数
        /// </summary>
        public static int[] MTRandomNumbers(int num, int max, MersenneTwisterRandom mtRandom)
        {
            if (num < 0 || max < 0 || num > max)
            {
                return(null);
            }

            int[] result = new int[num];
            int[] seed   = new int[max];

            for (int i = 0; i < max; i++)
            {
                seed[i] = i;
            }

            for (int i = 0; i < num; i++)
            {
                int index = mtRandom.Next(0, max - i);
                result[i]   = seed[index];
                seed[index] = seed[num - i - 1];
            }

            return(result);
        }
Пример #5
0
 public WeightRandomGenerator(MersenneTwisterRandom mtRandom)
 {
     this.mtRandom = mtRandom;
 }
Пример #6
0
 public static int MTRandom(int min, int max, MersenneTwisterRandom mtRandom)
 {
     return(mtRandom.Next(min, max));
 }
Пример #7
0
        /// <summary>
        /// 主键权重随机
        /// </summary>
        public static List <string> MTRandomKey(List <KeyValuePair <string, int> > weights, int count, MersenneTwisterRandom mtRandom)
        {
            int weightCount = weights.Count();

            if (weightCount == 0 || count <= 0)
            {
                return(null);
            }

            List <int> weightValues = new List <int>(weightCount);

            for (int i = 0; i < weightCount; i++)
            {
                weightValues.Add(weights[i].Value);
            }

            List <int> resultIndexes = MTRandomIndex(weightValues, count, mtRandom);

            if (resultIndexes.Count() != count)
            {
                return(null);
            }

            List <string> resultKeys = new List <string>(count);

            for (int i = 0; i < count; i++)
            {
                resultKeys.Add(weights[resultIndexes[i]].Key);
            }

            return(resultKeys);
        }