随机加权选择

考虑以下代表经纪人的类:


public class Broker

{

    public string Name = string.Empty;

    public int Weight = 0;


    public Broker(string n, int w)

    {

        this.Name = n;

        this.Weight = w;

    }

}

考虑到它们的权重,我想从一个数组中随机选择一个Broker。


您如何看待下面的代码?


class Program

    {

        private static Random _rnd = new Random();


        public static Broker GetBroker(List<Broker> brokers, int totalWeight)

        {

            // totalWeight is the sum of all brokers' weight


            int randomNumber = _rnd.Next(0, totalWeight);


            Broker selectedBroker = null;

            foreach (Broker broker in brokers)

            {

                if (randomNumber <= broker.Weight)

                {

                    selectedBroker = broker;

                    break;

                }


                randomNumber = randomNumber - broker.Weight;

            }


            return selectedBroker;

        }



        static void Main(string[] args)

        {

            List<Broker> brokers = new List<Broker>();

            brokers.Add(new Broker("A", 10));

            brokers.Add(new Broker("B", 20));

            brokers.Add(new Broker("C", 20));

            brokers.Add(new Broker("D", 10));


            // total the weigth

            int totalWeight = 0;

            foreach (Broker broker in brokers)

            {

                totalWeight += broker.Weight;

            }


            while (true)

            {

                Dictionary<string, int> result = new Dictionary<string, int>();


                Broker selectedBroker = null;


                for (int i = 0; i < 1000; i++)

                {

                    selectedBroker = GetBroker(brokers, totalWeight);

                    if (selectedBroker != null)

                    {

                        if (result.ContainsKey(selectedBroker.Name))

                        {

                            result[selectedBroker.Name] = result[selectedBroker.Name] + 1;

                        }



我不太自信 当我运行此代码时,经纪人A总是比经纪人D获得更多的匹配,而且它们的权重相同。


有没有更准确的算法?


MMMHUHU
浏览 523回答 4
4回答

慕姐4208626

class Program{&nbsp; &nbsp; static void Main(string[] args)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var books = new List<Book> {&nbsp; &nbsp; &nbsp; &nbsp; new Book{Isbn=1,Name="A",Weight=1},&nbsp; &nbsp; &nbsp; &nbsp; new Book{Isbn=2,Name="B",Weight=100},&nbsp; &nbsp; &nbsp; &nbsp; new Book{Isbn=3,Name="C",Weight=1000},&nbsp; &nbsp; &nbsp; &nbsp; new Book{Isbn=4,Name="D",Weight=10000},&nbsp; &nbsp; &nbsp; &nbsp; new Book{Isbn=5,Name="E",Weight=100000}};&nbsp; &nbsp; &nbsp; &nbsp; Book randomlySelectedBook = WeightedRandomization.Choose(books);&nbsp; &nbsp; }}public static class WeightedRandomization{&nbsp; &nbsp; public static T Choose<T>(List<T> list) where T : IWeighted&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (list.Count == 0)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return default(T);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; int totalweight = list.Sum(c => c.Weight);&nbsp; &nbsp; &nbsp; &nbsp; Random rand = new Random();&nbsp; &nbsp; &nbsp; &nbsp; int choice = rand.Next(totalweight);&nbsp; &nbsp; &nbsp; &nbsp; int sum = 0;&nbsp; &nbsp; &nbsp; &nbsp; foreach (var obj in list)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = sum; i < obj.Weight + sum; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (i >= choice)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return obj;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum += obj.Weight;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return list.First();&nbsp; &nbsp; }}public interface IWeighted{&nbsp; &nbsp; int Weight { get; set; }}public class Book : IWeighted{&nbsp; &nbsp; public int Isbn { get; set; }&nbsp; &nbsp; public string Name { get; set; }&nbsp; &nbsp; public int Weight { get; set; }}
打开App,查看更多内容
随时随地看视频慕课网APP