Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

395
Views
does C# have something equivalent to Pythons random.choices()

I'm trying to do choices based on their weight/probability

this is what I had in python:

import random

myChoiceList = ["Attack", "Heal", "Amplify", "Defense"]
myWeights = [70, 0, 15, 15] // % probability = 100% Ex. Attack has 70% of selection

print(random.choices(myChoicelist , weights = myWeights, k = 1))

I want to do the same thing in c#, how does one do that? does C# have any methods similar to random.choices() all I know is random.Next()

*this python code works fine randome.choice takes in (sequence, weights, k) sequence: values, weights: A list were you can weigh the possibility for each value, k: the length of the returned list,

I'm looking to do the same for C#, choose values based on there probability

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

There is nothing built into C# like this, however, it's not that hard to add an extension method to recreate the same basic behavior:

static class RandomUtils
{
    public static string Choice(this Random rnd, IEnumerable<string> choices, IEnumerable<int> weights)
    {
        var cumulativeWeight = new List<int>();
        int last = 0;
        foreach (var cur in weights)
        {
            last += cur;
            cumulativeWeight.Add(last);
        }
        int choice = rnd.Next(last);
        int i = 0;
        foreach (var cur in choices)
        {
            if (choice < cumulativeWeight[i])
            {
                return cur;
            }
            i++;
        }
        return null;
    }
}

Then you can call it in a similar way as the Python version:

string[] choices = { "Attack", "Heal", "Amplify", "Defense" };
int[] weights = { 70, 0, 15, 15 };

Random rnd = new Random();
Console.WriteLine(rnd.Choice(choices, weights));
over 4 years ago · Santiago Trujillo Report

0

you can get random.next(0,100), then choose the relevant item with a simple switch case or something. your domains will be like this , [0-70 , 70-85, 85-100]. let me know if you need full code.

    Random ran = new Random();
    int probability = ran.Next(0, 100);
    string s;
    if (probability == 0)
        s = "Heal";
    else if (probability <= 70)
        s = "Attack";
    else if (probability <= 85)
        s = "Amplify";
    else if (probability <= 100)
        s = "Defense";
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!