⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 roulette.cs

📁 c#编写
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace GA_RPS.GAKernel
{
    class Roulette
    {
        private bool[] ballot;
        private int left;

        double ranNum = 0.0;
        double haveRaned = 0.0;

        //private static int c=0;

        public int[] roll(int num, double[] prptn) // prptn is short for proportion.
        {
            int[] ret = new int[num];
            //Random ran = new Random();
            for (int i = 0; i < num; i++)
            {
                this.randomize();
                //MessageBox.Show(Convert.ToString(ranNum),Convert.ToString(c+1));
                double temp = 0;
                for (int j = 0; j < prptn.Length; j++)
                {
                    if (ranNum > temp && ranNum <= prptn[j] + temp)
                    {
                        ret[i] = j;
                        break;
                    }
                    temp += prptn[j];
                }
                //c++;
            }
            return ret;
        }

        public int ranRPS()
        {
            double[] d = new double[3];
            d[0] = 1.0 / 3.0;
            d[1] = 1.0 / 3.0;
            d[2] = 1.0 / 3.0;
            int[] i = roll(1, d);
            return i[0];
        }

        public int ranRPS(int exclusion)
        {
            resetBallot(3);
            ballot[exclusion] = true;
            return lot();
        }

        public void resetBallot(int num)
        {
            ballot = new bool[num];
            left = num;
        }

        public int lot()
        {
            //Random r = new Random();
            //int chosen = r.Next(left);
            int chosen = randomize(left);
            int count = 0;              // none chosen ones visit count
            int k = -1;                 // visit count
            do
            {
                if (!ballot[++k]) count++;
            } while (count < chosen + 1 && k < ballot.Length - 1);
            ballot[k] = true;
            left--;
            return k;
        }

        public int randomize(int max)
        {
            int ret = 0;
            while (ranNum == haveRaned)
            {
                ranNum = new Random().NextDouble();
            }
            ret = (int)(ranNum * max);
            haveRaned = ranNum;

            return ret;
        }

        public double randomize()
        {
            while (ranNum == haveRaned)
            {
                ranNum = new Random().NextDouble();
            }
            haveRaned = ranNum;
            return ranNum;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -