Skip to main content
optimized code
Source Link
public static Random random = new Random(DateTime.Now.Millisecond);
public int chooseWithChance(params int[] args)
    {
        /*
         * This method takes number of chances and randomly chooses
         * one of them considering their chance to be choosen.    
         * e.g. 
         *   chooseWithChance(10,99) will most probably (%99) return 1 
 since 99 is the second parameter.
     *   chooseWithChance(99,1) will most probably (%99) return 0 
 since 99 is the first parameter   *   chooseWithChance(0,100) will always return 1.
         *   chooseWithChance(100,0) will always return 0.
         *   chooseWithChance(67,0) will always return 0.
         */
        int argCount = args.Length;
        int sumOfChances = 0;
    
        for (int i = 0; i < argCount; i++) {
            sumOfChances += args[i];
        }
    
    int random = new Random(DateTime.Now.Millisecond)double randomDouble = random.NextNextDouble(sumOfChances); * sumOfChances;
                 
   
      while ((randomsumOfChances -=args[argCount-1])> 0randomDouble)
    {
    {
    argCount--;
        sumOfChances -= args[argCount -1];
            argCount--;
        }
    
        return argCount-1;
    }
string[] fruits = new string[] { "apple", "orange", "lemon" };
int choosenOne = chooseWithChance(498,21,1);
Console.WriteLine(fruits[choosenOne]);

if you give weight as (1,1,98) itThe above code will most probably (%98) return 20 which is index for 'lemon''apple' for yourthe given array.

Also, this code tests the method provided above:

Console.WriteLine("Start...");
int flipCount = 100;
int headCount = 0;
int tailsCount = 0;
                
for (int i=0; i< flipCount; i++) {
    if (chooseWithChance(50,50) == 0)
        headCount++;
    else
        tailsCount++;
}
     
Console.WriteLine("Head count:"+ headCount);
Console.WriteLine("Tails count:"+ tailsCount);

It gives an output something like that:

Start...
Head count:52
Tails count:48
public int chooseWithChance(params int[] args)
{
    /*
     * This method takes number of chances and randomly chooses
     * one of them considering their chance to be choosen.    
     * e.g. 
     *   chooseWithChance(1,99) will most probably (%99) return 1 since 99 is the second parameter.
     *   chooseWithChance(99,1) will most probably (%99) return 0 since 99 is the first parameter.     
     */
    int argCount = args.Length;
    int sumOfChances = 0;
 
    for (int i = 0; i < argCount; i++) {
        sumOfChances += args[i];
    }

    int random = new Random(DateTime.Now.Millisecond).Next(sumOfChances);
                   
     while ((random -=args[argCount-1])> 0)
    {
        argCount--;
        sumOfChances -= args[argCount -1];
    }
 
    return argCount-1;
}
string[] fruits = new string[] { "apple", "orange", "lemon" };
int choosenOne = chooseWithChance(4,2,1);
Console.WriteLine(fruits[choosenOne]);

if you give weight as (1,1,98) it will most probably (%98) return 2 which is index for 'lemon' for your array.

public static Random random = new Random(DateTime.Now.Millisecond);
public int chooseWithChance(params int[] args)
    {
        /*
         * This method takes number of chances and randomly chooses
         * one of them considering their chance to be choosen.    
         * e.g. 
         *   chooseWithChance(0,99) will most probably (%99) return 1 
         *   chooseWithChance(99,1) will most probably (%99) return 0 
         *   chooseWithChance(0,100) will always return 1.
         *   chooseWithChance(100,0) will always return 0.
         *   chooseWithChance(67,0) will always return 0.
         */
        int argCount = args.Length;
        int sumOfChances = 0;
    
        for (int i = 0; i < argCount; i++) {
            sumOfChances += args[i];
        }
    
        double randomDouble = random.NextDouble() * sumOfChances;
                 
        while (sumOfChances > randomDouble)
        {
            sumOfChances -= args[argCount -1];
            argCount--;
        }
    
        return argCount-1;
    }
string[] fruits = new string[] { "apple", "orange", "lemon" };
int choosenOne = chooseWithChance(98,1,1);
Console.WriteLine(fruits[choosenOne]);

The above code will most probably (%98) return 0 which is index for 'apple' for the given array.

Also, this code tests the method provided above:

Console.WriteLine("Start...");
int flipCount = 100;
int headCount = 0;
int tailsCount = 0;
                
for (int i=0; i< flipCount; i++) {
    if (chooseWithChance(50,50) == 0)
        headCount++;
    else
        tailsCount++;
}
     
Console.WriteLine("Head count:"+ headCount);
Console.WriteLine("Tails count:"+ tailsCount);

It gives an output something like that:

Start...
Head count:52
Tails count:48
correction of random caller.
Source Link

This gist is doing exactly what you are asking for.

public int chooseWithChance(params int[] args)
{
    /*
     * This method takes number of chances and randomly chooses
     * one of them considering their chance to be choosen.    
     * e.g. 
     *   chooseWithChance(1,99) will most probably (%99) return 1 since 99 is the second parameter.
     *   chooseWithChance(99,1) will most probably (%99) return 0 since 99 is the first parameter.     
     */
    int argCount = args.Length;
    int sumOfChances = 0;
 
    for (int i = 0; i < argCount; i++) {
        sumOfChances += args[i];
    } 

    int random = new Random(DateTime.Now.Millisecond).Next(sumOfChances);
           
     int rnd2 = rnd.Next(sumOfChances);
 
    while ((rnd2random -=args[argCount-1])> 0)
    {
        argCount--;
        sumOfChances -= args[argCount -1];
    }
 
    return argCount-1;
}

you can use it like that:

string[] fruits = new string[] { "apple", "orange", "lemon" };
int choosenOne = chooseWithChance(4,2,1);
Console.WriteLine(fruits[choosenOne]);

if you give weight as (1,1,98) it will most probably (%98) return 2 which is index for 'lemon' for your array.

This gist is doing exactly what you are asking for.

public int chooseWithChance(params int[] args)
{
    /*
     * This method takes number of chances and randomly chooses
     * one of them considering their chance to be choosen.    
     * e.g. 
     *   chooseWithChance(1,99) will most probably (%99) return 1 since 99 is the second parameter.
     *   chooseWithChance(99,1) will most probably (%99) return 0 since 99 is the first parameter.     
     */
    int argCount = args.Length;
    int sumOfChances = 0;
 
    for (int i = 0; i < argCount; i++) {
        sumOfChances += args[i];
    }
                
     int rnd2 = rnd.Next(sumOfChances);
 
    while ((rnd2-=args[argCount-1])> 0)
    {
        argCount--;
        sumOfChances -= args[argCount -1];
    }
 
    return argCount-1;
}

you can use it like that:

string[] fruits = new string[] { "apple", "orange", "lemon" };
int choosenOne = chooseWithChance(4,2,1);
Console.WriteLine(fruits[choosenOne]);

if you give weight as (1,1,98) it will most probably (%98) return 2 which is index for 'lemon' for your array.

This gist is doing exactly what you are asking for.

public int chooseWithChance(params int[] args)
{
    /*
     * This method takes number of chances and randomly chooses
     * one of them considering their chance to be choosen.    
     * e.g. 
     *   chooseWithChance(1,99) will most probably (%99) return 1 since 99 is the second parameter.
     *   chooseWithChance(99,1) will most probably (%99) return 0 since 99 is the first parameter.     
     */
    int argCount = args.Length;
    int sumOfChances = 0;
 
    for (int i = 0; i < argCount; i++) {
        sumOfChances += args[i];
    } 

    int random = new Random(DateTime.Now.Millisecond).Next(sumOfChances);
                   
    while ((random -=args[argCount-1])> 0)
    {
        argCount--;
        sumOfChances -= args[argCount -1];
    }
 
    return argCount-1;
}

you can use it like that:

string[] fruits = new string[] { "apple", "orange", "lemon" };
int choosenOne = chooseWithChance(4,2,1);
Console.WriteLine(fruits[choosenOne]);

if you give weight as (1,1,98) it will most probably (%98) return 2 which is index for 'lemon' for your array.

Source Link

This gist is doing exactly what you are asking for.

public int chooseWithChance(params int[] args)
{
    /*
     * This method takes number of chances and randomly chooses
     * one of them considering their chance to be choosen.    
     * e.g. 
     *   chooseWithChance(1,99) will most probably (%99) return 1 since 99 is the second parameter.
     *   chooseWithChance(99,1) will most probably (%99) return 0 since 99 is the first parameter.     
     */
    int argCount = args.Length;
    int sumOfChances = 0;
 
    for (int i = 0; i < argCount; i++) {
        sumOfChances += args[i];
    }
                
    int rnd2 = rnd.Next(sumOfChances);
 
    while ((rnd2-=args[argCount-1])> 0)
    {
        argCount--;
        sumOfChances -= args[argCount -1];
    }
 
    return argCount-1;
}

you can use it like that:

string[] fruits = new string[] { "apple", "orange", "lemon" };
int choosenOne = chooseWithChance(4,2,1);
Console.WriteLine(fruits[choosenOne]);

if you give weight as (1,1,98) it will most probably (%98) return 2 which is index for 'lemon' for your array.

Post Made Community Wiki by ramazan polat