jeudi 21 janvier 2021

How do I generate a rule for more than one option in Bogus?

I have a rule to select an option at random:

    .RuleFor(c=>field, x=>x.PickRandom("Option1", "Option2", "Option3", "Option4"))

With a default value, I can alter the probability of one of the items. I would like to set the probabilities of all four items. Option1 would be picked 50% of the time, Option2 would be picked 30%, then 15% for Option3, and 5% for Option4.

I would like to use a WeightedRandom:

    .RuleFor(c=>field, x=>PickWeightedRandom(valueArray, weightArray)

There is no such method as PickWeightedRandom, but WeightedRandom is a method in the Randomizer class. How do I get the WeightedRandom to work?

Aucun commentaire:

Enregistrer un commentaire