SoFunction
Updated on 2025-03-06

c# algorithm code for obtaining the same probability random number

These days, I was working on a lottery software for the company's annual meeting. When I started working on it, I thought the algorithm was very simple. I put the employee's data into the list, and used the list's label as the random number to be obtained. Based on the random number obtained, I decided who won the prize. Later tests found that the distribution of random numbers was very uneven. Later I learned that the random numbers obtained by computers are pseudo-random numbers. When the lottery speed is very fast, the random numbers obtained are very uneven, so a delay must be added in each lottery. Later, the algorithm was redesigned and finally implemented.

The principle of the algorithm is a bit similar to the process of binary search. The probability of drawing the front and back of a coin is the same. When the number of sampling increases infinitely, the probability of drawing is 50%.

The code is as follows:

Copy the codeThe 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", "yes", "one", "team" };

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

            }
            else
            {
                n = (n - 1) / 2;
                //s = getRandom() + s;
            }
            s = getRandom() + s;
            (20);
            GRandom(n);
            //(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++)
            {
                += Estimate(200) + ";";
                s = "";
            }
        }
    }

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

Copy the codeThe code is as follows:

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

        public int getRandom()
        {
//string[] arr = new string[5] { "we", "yes", "one", "team" };

            //Random r = new Random();
            int num = 2;
            int choose = (num);
            return choose;
            //(arr[choose].ToString());
        }
        public string GRandom(int n)
        {
            //if()
            if (n == 0)
            {
                //s = getRandom() + s;
                //(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++)
            {
                = Estimate(200);
                s = "";
            }

//The following is the test
            //int a = 0, b = 0, c = 0, d = 0, f = 0;
            //for (int i = 0; i < 1000; i++)
            //{
            //    // = 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;

            //    }
            //    = a;
            //    = b;
            //    = c;
            //    = d;
            //    = f;
            //}
        }
    }
}