<legend id='ILgZT'><style id='ILgZT'><dir id='ILgZT'><q id='ILgZT'></q></dir></style></legend>

    <tfoot id='ILgZT'></tfoot>
  1. <i id='ILgZT'><tr id='ILgZT'><dt id='ILgZT'><q id='ILgZT'><span id='ILgZT'><b id='ILgZT'><form id='ILgZT'><ins id='ILgZT'></ins><ul id='ILgZT'></ul><sub id='ILgZT'></sub></form><legend id='ILgZT'></legend><bdo id='ILgZT'><pre id='ILgZT'><center id='ILgZT'></center></pre></bdo></b><th id='ILgZT'></th></span></q></dt></tr></i><div id='ILgZT'><tfoot id='ILgZT'></tfoot><dl id='ILgZT'><fieldset id='ILgZT'></fieldset></dl></div>

  2. <small id='ILgZT'></small><noframes id='ILgZT'>

      <bdo id='ILgZT'></bdo><ul id='ILgZT'></ul>

    1. 用于从每个元素具有不同概率的列表中进行选择

      时间:2023-10-07

      <small id='MY30s'></small><noframes id='MY30s'>

    2. <i id='MY30s'><tr id='MY30s'><dt id='MY30s'><q id='MY30s'><span id='MY30s'><b id='MY30s'><form id='MY30s'><ins id='MY30s'></ins><ul id='MY30s'></ul><sub id='MY30s'></sub></form><legend id='MY30s'></legend><bdo id='MY30s'><pre id='MY30s'><center id='MY30s'></center></pre></bdo></b><th id='MY30s'></th></span></q></dt></tr></i><div id='MY30s'><tfoot id='MY30s'></tfoot><dl id='MY30s'><fieldset id='MY30s'></fieldset></dl></div>
      1. <legend id='MY30s'><style id='MY30s'><dir id='MY30s'><q id='MY30s'></q></dir></style></legend>
          <tbody id='MY30s'></tbody>

                <bdo id='MY30s'></bdo><ul id='MY30s'></ul>
                <tfoot id='MY30s'></tfoot>

              • 本文介绍了用于从每个元素具有不同概率的列表中进行选择的 C++ 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我有一个结构数组,结构中的一个字段是浮点数.我想选择一种结构,其中选择它的概率与浮点数的值有关.即

                I have an array of structs and one of the fields in the struct is a float. I want to pick one of the structs where the probability of picking it is relative to the value of the float. ie

                struct s{
                  float probability;
                  ...
                }
                
                s sArray[50];
                

                决定选择哪个的最快方法是什么?有这个功能吗?如果我知道所有概率字段的总和(注意它不会是 1),那么我可以遍历每个 s 并将 probability/total_probability 与随机数进行比较,更改每个 s 的随机数?即

                What is the fastest way to decide which s to pick? Is there a function for this? If I knew the sum of all the probability fields (Note it will not be 1), then could I iterate through each s and compare probability/total_probability with a random number, changing the random number for each s? ie

                if( (float) (rand() / RAND_MAX) < probability)...
                

                推荐答案

                float p = (rand() / static_cast<float>(RAND_MAX)) * total_probability;
                s* current = &sArray[0];
                while ( (p -= current->probability) > 0)
                    ++current;
                // `current` now points to your chosen target
                

                这篇关于用于从每个元素具有不同概率的列表中进行选择的 C++ 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:rand() 和 random() 函数有什么区别? 下一篇:什么是游戏的好的随机数生成器?

                相关文章

                最新文章

                <legend id='udN74'><style id='udN74'><dir id='udN74'><q id='udN74'></q></dir></style></legend>
                  • <bdo id='udN74'></bdo><ul id='udN74'></ul>

                  <small id='udN74'></small><noframes id='udN74'>

                  1. <i id='udN74'><tr id='udN74'><dt id='udN74'><q id='udN74'><span id='udN74'><b id='udN74'><form id='udN74'><ins id='udN74'></ins><ul id='udN74'></ul><sub id='udN74'></sub></form><legend id='udN74'></legend><bdo id='udN74'><pre id='udN74'><center id='udN74'></center></pre></bdo></b><th id='udN74'></th></span></q></dt></tr></i><div id='udN74'><tfoot id='udN74'></tfoot><dl id='udN74'><fieldset id='udN74'></fieldset></dl></div>
                  2. <tfoot id='udN74'></tfoot>