How To Make Selection Random Based On Percentage

First of all: the probabilities provided don't add up to 100%:

50% + 20% + 5% = 75%

So you have to check these values. You may want to generate these per cents:

// Simplest, but not thread safe
private static Random s_Random = new Random();

...
int perCent = s_Random.Next(0, 100);

if (perCent < 50)               //  0 .. 49
{
    // return Item of size 1
}
else if (perCent < 50 + 20)     // 50 .. 69
{
    // return Item of size 5
}
else if (perCent < 50 + 20 + 5) // 70 .. 74 
{
    // return Item of size 10
} 
...

Use my method. It is simple and easy-to-understand. I don't count portion in range 0...1, i just use "Probabilityp Pool" (sounds cool, yeah?) I make a list with all elements i want choose from. Every element has its own chance. It is useful to set the most common element chance = 100, so most rare elements would be 60 or 50.

At circle diagram you can see weight of every element in pool

Here you can see an implementing of accumulative probability for roulette

`

// Some c`lass or struct for represent items you want to roulette
public class Item
{
    public string name; // not only string, any type of data
    public int chance;  // chance of getting this Item
}

public class ProportionalWheelSelection
{
    public static Random rnd = new Random();

    // Static method for using from anywhere. You can make its overload for accepting not only List, but arrays also: 
    // public static Item SelectItem (Item[] items)...
    public static Item SelectItem(List<Item> items)
    {
        // Calculate the summa of all portions.
        int poolSize = 0;
        for (int i = 0; i < items.Count; i++)
        {
            poolSize += items[i].chance;
        }

        // Get a random integer from 0 to PoolSize.
        int randomNumber = rnd.Next(0, poolSize) + 1;

        // Detect the item, which corresponds to current random number.
        int accumulatedProbability = 0;
        for (int i = 0; i < items.Count; i++)
        {
            accumulatedProbability += items[i].chance;
            if (randomNumber <= accumulatedProbability)
                return items[i];
        }
        return null;    // this code will never come while you use this programm right :)
    }
}

// Example of using somewhere in your program:
        static void Main(string[] args)
        {
            List<Item> items = new List<Item>();
            items.Add(new Item() { name = "Anna", chance = 100});
            items.Add(new Item() { name = "Alex", chance = 125});
            items.Add(new Item() { name = "Dog", chance = 50});
            items.Add(new Item() { name = "Cat", chance = 35});

            Item newItem = ProportionalWheelSelection.SelectItem(items);
        }

Tags:

C#

Random