c obtains the algorithm code of the same probability random number

  • 2020-06-07 05:14:12
  • OfStack

These days, When I started to do a lottery software for the company's annual meeting, I thought the algorithm was very simple. I put the data of employees into list, took the number of list as the random number to be obtained, and determined who won the lottery according to the random number obtained. Tests later found that the distribution of random Numbers was very uneven. Later I learned that the original random Numbers obtained by the computer were all pseudorandom Numbers. When the speed of the lucky draw was very fast, the random Numbers obtained were very uneven, so a delay should be added in each lucky draw. The algorithm was redesigned and eventually implemented.

The algorithm works a little bit like a two-point search. The probability of a coin being drawn heads or tails is 1, and when the number of samples increases infinitely, the probability of a coin being drawn is 50%.

The code is as follows:


public partial class MainWindow : Window
    {
        string s;
        int number;
        public MainWindow()
        {
            InitializeComponent();
        }
        public int getRandom()
        {
            //string[] arr = new string[5] { " we ", " is ", "1", " a "," team " };
            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 = "";
            }
        }
    }

The above algorithm is not very good. Cancel the delay and set the random object to the global variable. The modified version code is as follows:


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

        public int getRandom()
        {
            //string[] arr = new string[5] { " we ", " is ", "1", " a "," team " };
            //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 = "";
            }

        // Here are the tests 
            //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;
            //}
        }
    }
}


Related articles: