I have 3 variables and each one has their own percentage to win out of 99.99...%. (The 3 percentages don't act up to 100% perfectly)When button1 is clicked, it will go through 100,000 simulations and afterwards it will output how many times each variable won in a MessageBox.I would like some help optimizing my code. I feel like it's too clunky and maybe there's an easier way to complete the same task with far less lines.private Random rand = new Random();private int aWon = 0;private int bWon = 0;private int cWon = 0;private void button1_Click(object sender, EventArgs e){    // loop 100000 times for an accurate sample size output    for (int i = 0; i < 100000; i++)    {        // randomNumber == a random number between 0 and 0.9999999999999999        double randomNumber = rand.NextDouble() * (0.9999999999999999 - 0) + 0;        // a + b + c == 0.9999999999999999        double a = 0.4917127071823204; // ~49% to win        double b = 0.3922651933701657; // ~39% to win        double c = 0.1160220994475138; // ~11% to win        // if randomNumber is less than or equal to 0.9999999999999999        // AND greater than 0.5082872928176795        //        // 0.9999999999999999   -   0.5082872928176795   =   0.4917127071823204 ( a's value )        if (randomNumber <= a + b + c && randomNumber > b + c)        {            aWon += 1;        }        // if randomNumber is less than or equal to 0.5082872928176795        // AND greater than 0.1160220994475138        //        // 0.5082872928176795   -   0.1160220994475138   =   0.3922651933701657 ( b's value )        if (randomNumber <= b + c && randomNumber > c)        {            bWon += 1;        }        // if randomNumber is less than or equal to 0.1160220994475138        //        // 0.1160220994475138   -   0   =   0.1160220994475138 ( c's value )        if (randomNumber <= c)        {            cWon += 1;        }    }    // show how many times each variable won    MessageBox.Show(aWon + \n + bWon + \n + cWon);}
Random Winner Based on Percentage Weighting
c#
The variables a, b, and c are always the same, so it makes sense to move them out of the loop. The same goes for sums of these variables, such as a + b + c, and b + c.The conditions used to evaluate randomNumber can be simplified,and it will be good to join them with else if to avoid unnecessarily repeated evaluations. Something like this:double a = 0.4917127071823204; // ~49% to windouble b = 0.3922651933701657; // ~39% to windouble c = 0.1160220994475138; // ~11% to windouble min = 0;double max = a + b +c;double ab = a + b;for (int i = 0; i < 100000; i++){    double randomNumber = rand.NextDouble() * (max - min) + min;    if (randomNumber <= a)    {        aWon += 1;    }    else if (randomNumber <= ab)    {        bWon += 1;    }    else    {        cWon += 1;    }}