Exemplo n.º 1
0
        public int[] Process(int rcount)
        {
            int[] results = new int[rcount];
            for (int i = 0; i < rcount; i++)
            {
                results[i] = 0;
            }

            if (datas.Count / 2 <= rcount) //如果数据过少
            {
                for (int i = 0; i < datas.Count / 2; i++)
                {
                    results[i] = datas[i * 2];
                }
            }
            else
            {
                int total = 0;
                for (int i = 1; i < datas.Count; i += 2)
                {
                    total += datas[i];
                }

                for (int i = 0; i < rcount; ++i)
                {
                    while (true)
                    {
                        int var     = MathTool.GetRandom(total);
                        int tempsum = 0;
                        for (int j = 0; j < datas.Count; j += 2)
                        {
                            tempsum += datas[j + 1];
                            if (var < tempsum)
                            {
                                var = datas[j];
                                break;
                            }
                        }

                        bool flag = false;
                        for (int j = 0; j < i; j++)
                        {
                            if (var == results[j])
                            {
                                flag = true;
                            }
                        }
                        if (!flag)
                        {
                            results[i] = var;
                            break;
                        }
                    }
                }
            }
            return(results);
        }
Exemplo n.º 2
0
 public static void RandomShuffle(List <int> list)
 {
     for (int i = 0; i < list.Count; ++i)
     {
         int var  = MathTool.GetRandom(list.Count);
         int temp = list[i];
         list[i]   = list[var];
         list[var] = temp;
     }
 }