第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

隨機(jī)加權(quán)選擇

隨機(jī)加權(quán)選擇

MMMHUHU 2019-12-21 13:06:38
考慮以下代表經(jīng)紀(jì)人的類:public class Broker{    public string Name = string.Empty;    public int Weight = 0;    public Broker(string n, int w)    {        this.Name = n;        this.Weight = w;    }}考慮到它們的權(quán)重,我想從一個(gè)數(shù)組中隨機(jī)選擇一個(gè)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;                        }我不太自信 當(dāng)我運(yùn)行此代碼時(shí),經(jīng)紀(jì)人A總是比經(jīng)紀(jì)人D獲得更多的匹配,而且它們的權(quán)重相同。有沒有更準(zhǔn)確的算法?
查看完整描述

4 回答

?
慕姐4208626

TA貢獻(xiàn)1852條經(jīng)驗(yàn) 獲得超7個(gè)贊

class Program

{

    static void Main(string[] args)

    {

        var books = new List<Book> {

        new Book{Isbn=1,Name="A",Weight=1},

        new Book{Isbn=2,Name="B",Weight=100},

        new Book{Isbn=3,Name="C",Weight=1000},

        new Book{Isbn=4,Name="D",Weight=10000},

        new Book{Isbn=5,Name="E",Weight=100000}};


        Book randomlySelectedBook = WeightedRandomization.Choose(books);

    }

}


public static class WeightedRandomization

{

    public static T Choose<T>(List<T> list) where T : IWeighted

    {

        if (list.Count == 0)

        {

            return default(T);

        }


        int totalweight = list.Sum(c => c.Weight);

        Random rand = new Random();

        int choice = rand.Next(totalweight);

        int sum = 0;


        foreach (var obj in list)

        {

            for (int i = sum; i < obj.Weight + sum; i++)

            {

                if (i >= choice)

                {

                    return obj;

                }

            }

            sum += obj.Weight;

        }


        return list.First();

    }

}


public interface IWeighted

{

    int Weight { get; set; }

}


public class Book : IWeighted

{

    public int Isbn { get; set; }

    public string Name { get; set; }

    public int Weight { get; set; }

}


查看完整回答
反對(duì) 回復(fù) 2019-12-21
  • 4 回答
  • 0 關(guān)注
  • 604 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)