Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

402
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda