Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

percentage random c#

`

// 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);
        }
Comment

PREVIOUS NEXT
Code Example
Csharp :: unity shader show object behind object 
Csharp :: how to play a random sound at the position that you want in unity 
Csharp :: create viewport revit api 
Csharp :: c# dubble comment 
Csharp :: c# copy each property 
Csharp :: wpf line intersect rectangle 
Csharp :: how can datetimepicker accept hour as well 
Csharp :: Reading a date from xlsx using open xml sdk 
Csharp :: how to make a variable unity 
Csharp :: unity async await 
Csharp :: index in foreach in c# 
Csharp :: c# datagridview count value 
Csharp :: unity overlapspherenonalloc 
Csharp :: swagger skip endpoint .net core 
Csharp :: euler angles to quaternion unity 
Csharp :: C# get the last item of the array 
Csharp :: c# get regedit value 
Csharp :: Unity inverse kinematics nothing is happening 
Csharp :: use different database with entitymanagerfactory 
Csharp :: C# program to find the longest Palindrome in a string. 
Html :: marquee speed 
Html :: create a mailto link html 
Html :: how to stop download option in video tag of HTML 
Html :: disable html form input autocomplete autofill 
Html :: html video disable right click 
Html :: link icon html 
Html :: bootstrap div vertical center 
Html :: iframe adjust pdf zoom 
Html :: vertical break html 
Html :: meta theme color 
ADD CONTENT
Topic
Content
Source link
Name
2+4 =