In this tutorial I show a method to randomly select something from a list but taking weights (different probabilities) into account. When I run this, Broker A always gets more hits than Broker D, and they have the same weight. Weighted Random Selection With Godot OctoWhen working in a game we frequently want to randomly select something, be it to generate loot or maybe pick a random tile to generate levels. Result = result + 1 Ĭonsole.WriteLine("A\t\t" + result) Ĭonsole.WriteLine("B\t\t" + result) Ĭonsole.WriteLine("C\t\t" + result) Ĭonsole.WriteLine("D\t\t" + result) If (result.ContainsKey(selectedBroker.Name)) SelectedBroker = GetBroker(brokers, totalWeight) Int randomNumber = _rnd.Next(0, totalWeight) totalWeight is the sum of all brokers' weight Public static Broker GetBroker(List brokers, int totalWeight) Private static Random _rnd = new Random() What do you think of the code below? class Program I'd like to randomly select a Broker from an array, taking into account their weights. , wn-1, return an index i in range(n) with probability proportional to wi. Consider the class below that represents a Broker: public class Broker def weightedchoice(weights, randomrandom): ''' Given a list of weights w0, w1.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |