程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> c#獲得雷同幾率隨機數的算法代碼

c#獲得雷同幾率隨機數的算法代碼

編輯:C#入門知識

c#獲得雷同幾率隨機數的算法代碼。本站提示廣大學習愛好者:(c#獲得雷同幾率隨機數的算法代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是c#獲得雷同幾率隨機數的算法代碼正文


這幾天在做公司年會的一個抽獎軟件,開端做的的時刻,以為算法是很簡略的,把員工的數據放進list裡,把list的標號作為須要獲得的隨機數,依據獲得的隨機數就肯定是誰中獎。後來測試發明,隨機數的散布長短常不平均的。後來才曉得,本來盤算機獲得的隨機數都是偽隨機數,當抽獎的速度異常快的時刻,獲得的隨機數長短常不平均的,所以在每次抽獎的時刻要添加延時。後來從新設盤算法,終究完成了。

算法道理跟二分查找的進程有點像。一枚硬幣抽中正、不和的幾率是一樣,當抽樣的次數無窮增多,抽中的幾率是50%。

代碼以下:


public partial class MainWindow : Window
    {
        string s;
        int number;
        public MainWindow()
        {
            InitializeComponent();
        }
        public int getRandom()
        {
            //string[] arr = new string[5] { "我們", "是", "一", "個","團隊" };

            Random r = new Random();
            int num = 2;
            int choose = r.Next(num);
            return choose;
            //MessageBox.Show(arr[choose].ToString());
        }
        public string GRandom(int n)
        {
            //if()
            if (n == 0)
            {
                //s = getRandom() + s;
                //System.Threading.Thread.Sleep(1);
                return s;
            }
            if (n % 2 == 0)
            {
                n = n / 2;

            }
            else
            {
                n = (n - 1) / 2;
                //s = getRandom() + s;
            }
            s = getRandom() + s;
            System.Threading.Thread.Sleep(20);
            GRandom(n);
            //System.Threading.Thread.Sleep(1);
            return s;
        }
        public Int32 Estimate(int n)
        {
            string num = GRandom(n);
            number = Convert.ToInt32(num, 2);
            if (number > n - 1)
            {
                //num = "";
                s = "";
                Estimate(n);
            }
            //else
            return number;
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < 100; i++)
            {
                label1.Content += Estimate(200) + ";";
                s = "";
            }
        }
    }

以上算法不長短常好,撤消延時,將random對象設置為全局變量。修正版代碼以下:

string s;
        int number;
        Random r = new Random();

        public int getRandom()
        {
            //string[] arr = new string[5] { "我們", "是", "一", "個","團隊" };

            //Random r = new Random();
            int num = 2;
            int choose = r.Next(num);
            return choose;
            //MessageBox.Show(arr[choose].ToString());
        }
        public string GRandom(int n)
        {
            //if()
            if (n == 0)
            {
                //s = getRandom() + s;
                //System.Threading.Thread.Sleep(1);
                return s;
            }
            if (n % 2 == 0)
            {
                n = n / 2;

            }
            else
            {
                n = (n - 1) / 2;
                //s = getRandom() + s;
            }
            s = getRandom() + s;
            GRandom(n);

            return s;
        }
        public Int32 Estimate(int n)
        {
            string num = GRandom(n);
            number = Convert.ToInt32(num, 2);
            if (number > n - 1)
            {
                //num = "";
                s = "";
                Estimate(n);
            }
            //else
            return number;
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < 1000; i++)
            {
                label1.Content = Estimate(200);
                s = "";
            }

        //以下為測試
            //int a = 0, b = 0, c = 0, d = 0, f = 0;
            //for (int i = 0; i < 1000; i++)
            //{
            //    //label1.Content = Estimate(2);
            //    int content = Estimate(5);
            //    s = "";

            //    switch (content)
            //    {
            //        case 0:
            //            a ++;
            //            break;
            //        case 1:
            //            b ++;
            //            break;
            //        case 2:
            //            c ++;
            //            break;
            //        case 3:
            //            d ++;
            //            break;
            //        case 4:
            //            f ++;
            //            break;

            //    }
            //    label1.Content = a;
            //    label2.Content = b;
            //    label3.Content = c;
            //    label4.Content = d;
            //    label5.Content = f;
            //}
        }
    }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved