Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

396
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda