pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

<legend id='eewyY'><style id='eewyY'><dir id='eewyY'><q id='eewyY'></q></dir></style></legend>
  1. <small id='eewyY'></small><noframes id='eewyY'>

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

      生成一個范圍內的N個隨機數,其總和為常數

      Generate N random numbers within a range with a constant sum(生成一個范圍內的N個隨機數,其總和為常數)
        <tbody id='YQjoE'></tbody>
    2. <tfoot id='YQjoE'></tfoot>
        1. <legend id='YQjoE'><style id='YQjoE'><dir id='YQjoE'><q id='YQjoE'></q></dir></style></legend>

          • <bdo id='YQjoE'></bdo><ul id='YQjoE'></ul>

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

                <i id='YQjoE'><tr id='YQjoE'><dt id='YQjoE'><q id='YQjoE'><span id='YQjoE'><b id='YQjoE'><form id='YQjoE'><ins id='YQjoE'></ins><ul id='YQjoE'></ul><sub id='YQjoE'></sub></form><legend id='YQjoE'></legend><bdo id='YQjoE'><pre id='YQjoE'><center id='YQjoE'></center></pre></bdo></b><th id='YQjoE'></th></span></q></dt></tr></i><div class="lvbbn7n" id='YQjoE'><tfoot id='YQjoE'></tfoot><dl id='YQjoE'><fieldset id='YQjoE'></fieldset></dl></div>
                本文介紹了生成一個范圍內的N個隨機數,其總和為常數的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                問題描述

                我想從 [a,b] 之間的特定分布(例如均勻隨機)中生成 N 個隨機數,它們的總和為常數 C.我嘗試了一些我能想到的解決方案,其中一些建議用于類似的線程,但它們中的大多數要么針對有限形式的問題工作,要么我無法證明結果仍然遵循所需的分布.

                I want to generate N random numbers drawn from a specif distribution (e.g uniform random) between [a,b] which sum to a constant C. I have tried a couple of solutions I could think of myself, and some proposed on similar threads but most of them either work for a limited form of problem or I can't prove the outcome still follows the desired distribution.

                我嘗試過的:生成 N 個隨機數,將它們全部除以它們的總和并乘以所需的常數.這似乎有效,但結果不遵循數字應在 [a:b] 內的規則.

                What I have tried: Generage N random numbers, divide all of them by the sum of them and multiply by the desired constant. This seems to work but the result does not follow the rule that the numbers should be within [a:b].

                生成 N-1 個隨機數加上 0 和所需的常數 C 并對其進行排序.然后計算每兩個連續數字之間的差值,差值就是結果.這再次與 C 相加,但具有與上一個方法相同的問題(范圍可以大于 [a:b].

                Generage N-1 random numbers add 0 and desired constant C and sort them. Then calculate the difference between each two consecutive nubmers and the differences are the result. This again sums to C but have the same problem of last method(the range can be bigger than [a:b].

                我還嘗試生成隨機數,并始終以保持所需總和和范圍的方式跟蹤最小值和最大值,并得出以下代碼:

                I also tried to generate random numbers and always keep track of min and max in a way that the desired sum and range are kept and come up with this code:

                bool generate(function<int(int,int)> randomGenerator,int min,int max,int len,int sum,std::vector<int> &output){
                    /**
                    * Not possible to produce such a sequence
                    */
                if(min*len > sum)
                    return false;
                if(max*len < sum)
                    return false;
                
                int curSum = 0;
                int left = sum - curSum;
                int leftIndexes = len-1;
                int curMax = left - leftIndexes*min;
                int curMin = left - leftIndexes*max;
                
                for(int i=0;i<len;i++){
                    int num = randomGenerator((curMin< min)?min:curMin,(curMax>max)?max:curMax);
                    output.push_back(num);
                    curSum += num;
                    left = sum - curSum;
                    leftIndexes--;
                    curMax = left - leftIndexes*min;
                    curMin = left - leftIndexes*max;
                }
                
                return true;
                }
                

                這似乎有效,但結果有時非常不準確,我認為它沒有遵循原始分布(例如均勻分布).例如:

                This seems to work but the results are sometimes very skewed and I don't think it's following the original distribution (e.g. uniform). E.g:

                //10 numbers within [1:10] which sum to 50:
                generate(uniform,1,10,10,50,output);
                //result:
                2,7,2,5,2,10,5,8,4,5 => sum=50
                //This looks reasonable for uniform, but let's change to 
                //10 numbers within [1:25] which sum to 50:
                generate(uniform,1,25,10,50,output);
                //result:
                24,12,6,2,1,1,1,1,1,1 => sum= 50
                

                注意輸出中有多少個.這聽起來可能合理,因為范圍更大.但它們看起來真的不像均勻分布.我不確定即使有可能實現我想要的,也許限制使問題無法解決.

                Notice how many ones exist in the output. This might sound reasonable because the range is larger. But they really don't look like a uniform distribution. I am not sure even if it is possible to achieve what I want, maybe the constraints are making the problem not solvable.

                推薦答案

                如果您希望樣本服從均勻分布,則問題簡化為生成總和 = 1 的 N 個隨機數.反過來,這是一個特殊的Dirichlet 分布的情況,但也可以使用指數分布更容易地計算.方法如下:

                In case you want the sample to follow a uniform distribution, the problem reduces to generate N random numbers with sum = 1. This, in turn, is a special case of the Dirichlet distribution but can also be computed more easily using the Exponential distribution. Here is how:

                1. 取一個統一的樣本 v1 ... vN,其中所有的 vi 都在 0 和 1 之間.
                2. 對于所有的 i,1<=i<=N,定義 ui := -ln vi(注意 ui> 0).
                3. 將 ui 標準化為 pi := ui/s 其中 s 是總和 u1+...+uN.
                1. Take a uniform sample v1 … vN with all vi between 0 and 1.
                2. For all i, 1<=i<=N, define ui := -ln vi (notice that ui > 0).
                3. Normalize the ui as pi := ui/s where s is the sum u1+...+uN.

                p1..pN 是均勻分布的(在dim N-1 的單純形中),它們的和為1.

                The p1..pN are uniformly distributed (in the simplex of dim N-1) and their sum is 1.

                您現在可以將這些 pi 乘以您想要的常數 C,然后通過對其他常數 A 求和來轉換它們

                You can now multiply these pi by the constant C you want and translate them by summing some other constant A like this

                qi := A + pi*C.

                qi := A + pi*C.

                編輯 3

                為了解決評論中提出的一些問題,讓我添加以下內容:

                In order to address some issues raised in the comments, let me add the following:

                • 為了確保最終的隨機序列落在區間 [a,b] 內,選擇上面的常數 A 和 C 作為 A := a 和 C := ba,即取 qi =a + pi*(ba).由于 pi 在 (0,1) 范圍內,所有 qi 都將在 [a,b] 范圍內.
                • 如果 vi 恰好為 0,則不能取(負)對數 -ln(vi) 因為 ln() 未定義為 0.概率此類事件的發生率極低.但是,為了確保不會發出錯誤信號,上面第 1 項中 v1 ... vN 的生成必須以特殊方式威脅任何 0 的出現:將 -ln(0) 視為 +infinity(記住:ln(x) -> -infinity 當 x->0 時).因此總和 s = +infinity,這意味著 pi = 1 和所有其他 pj = 0.沒有這個約定,序列 (0...1...0) 永遠不會被生成(非常感謝@Severin Pappadeux 的這個有趣的評論.)
                • 正如@Neil Slater 在問題所附的第四條評論中所解釋的那樣,從邏輯上講,不可能滿足原始框架的所有要求.因此,任何解決方案都必須將約束放寬到原始約束的適當子集.@Behrooz 的其他評論似乎證實這在這種情況下就足夠了.
                • To ensure that the final random sequence falls in the interval [a,b] choose the constants A and C above as A := a and C := b-a, i.e., take qi = a + pi*(b-a). Since pi is in the range (0,1) all qi will be in the range [a,b].
                • One cannot take the (negative) logarithm -ln(vi) if vi happens to be 0 because ln() is not defined at 0. The probability of such an event is extremely low. However, in order to ensure that no error is signaled the generation of v1 ... vN in item 1 above must threat any occurrence of 0 in a special way: consider -ln(0) as +infinity (remember: ln(x) -> -infinity when x->0). Thus the sum s = +infinity, which means that pi = 1 and all other pj = 0. Without this convention the sequence (0...1...0) would never be generated (many thanks to @Severin Pappadeux for this interesting remark.)
                • As explained in the 4th comment attached to the question by @Neil Slater it is logically impossible to fulfill all the requirements of the original framing. Therefore any solution must relax the constraints to a proper subset of the original ones. Other comments by @Behrooz seem to confirm that this would suffice in this case.

                編輯 2

                評論中又提出了一個問題:

                One more issue has been raised in the comments:

                為什么重新調整統一樣本還不夠?

                換句話說,我為什么要費心取負對數?

                原因是,如果我們只是重新縮放,那么結果樣本將不會均勻分布在 (0,1) 段(或 [a,b] 為最終樣本.)

                The reason is that if we just rescale then the resulting sample won't distribute uniformly across the segment (0,1) (or [a,b] for the final sample.)

                為了形象化,讓我們考慮 2D,即讓我們考慮 N=2 的情況.一個均勻樣本 (v1,v2) 對應于正方形中具有原點 (0,0) 和角點 (1,1) 的隨機點.現在,當我們將這樣一個點除以總和 s=v1+v2 歸一化時,我們所做的是將點投影到對角線上,如圖所示(請記住,對角線是 x + y = 1 線):

                To visualize this let's think 2D, i.e., let's consider the case N=2. A uniform sample (v1,v2) corresponds to a random point in the square with origin (0,0) and corner (1,1). Now, when we normalize such a point dividing it by the sum s=v1+v2 what we are doing is projecting the point onto the diagonal as shown in the picture (keep in mind that the diagonal is the line x + y = 1):

                但考慮到更靠近從 (0,0) 到 (1,1) 的主對角線的綠線比靠近 x 軸和 y 軸的橙色線長,投影往往會累積更多圍繞投影線(藍色)的中心,縮放樣本所在的位置.這表明簡單的縮放不會在所描繪的對角線上產生統一的樣本.另一方面,可以從數學上證明負對數確實產生了所需的均勻性.因此,與其復制粘貼數學證明,不如邀請每個人實現這兩種算法,并檢查結果圖是否與此答案描述的一樣.

                But given that green lines, which are closer to the principal diagonal from (0,0) to (1,1), are longer than orange ones, which are closer to the axes x and y, the projections tend to accumulate more around the center of the projection line (in blue), where the scaled sample lives. This shows that a simple scaling won't produce a uniform sample on the depicted diagonal. On the other hand, it can be proven mathematically that the negative logarithms do produce the desired uniformity. So, instead of copypasting a mathematical proof I would invite everyone to implement both algorithms and check that the resulting plots behave as this answer describes.

                (注意:此處 是一篇關于這個有趣主題的博客文章,適用于石油和天然氣行業)

                (Note: here is a blog post on this interesting subject with an application to the Oil & Gas industry)

                這篇關于生成一個范圍內的N個隨機數,其總和為常數的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                相關文檔推薦

                read input files, fastest way possible?(讀取輸入文件,最快的方法?)
                The easiest way to read formatted input in C++?(在 C++ 中讀取格式化輸入的最簡單方法?)
                Reading from .txt file into two dimensional array in c++(從 .txt 文件讀取到 C++ 中的二維數組)
                How to simulate a key press in C++(如何在 C++ 中模擬按鍵按下)
                Why doesn#39;t getline(cin, var) after cin.ignore() read the first character of the string?(為什么在 cin.ignore() 之后沒有 getline(cin, var) 讀取字符串的第一個字符?)
                What is the cin analougus of scanf formatted input?(scanf 格式輸入的 cin 類比是什么?)
                  <legend id='ZLsuk'><style id='ZLsuk'><dir id='ZLsuk'><q id='ZLsuk'></q></dir></style></legend>

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

                  • <tfoot id='ZLsuk'></tfoot>
                    <i id='ZLsuk'><tr id='ZLsuk'><dt id='ZLsuk'><q id='ZLsuk'><span id='ZLsuk'><b id='ZLsuk'><form id='ZLsuk'><ins id='ZLsuk'></ins><ul id='ZLsuk'></ul><sub id='ZLsuk'></sub></form><legend id='ZLsuk'></legend><bdo id='ZLsuk'><pre id='ZLsuk'><center id='ZLsuk'></center></pre></bdo></b><th id='ZLsuk'></th></span></q></dt></tr></i><div class="rfndxtz" id='ZLsuk'><tfoot id='ZLsuk'></tfoot><dl id='ZLsuk'><fieldset id='ZLsuk'></fieldset></dl></div>
                      <bdo id='ZLsuk'></bdo><ul id='ZLsuk'></ul>

                          <tbody id='ZLsuk'></tbody>
                        1. 主站蜘蛛池模板: 酵素生产厂家_酵素OEM_酵素加盟_酵素ODM_酵素原料厂家_厦门益力康 | SDG吸附剂,SDG酸气吸附剂,干式酸性气体吸收剂生产厂家,超过20年生产使用经验。 - 富莱尔环保设备公司(原名天津市武清县环保设备厂) | 乐泰胶水_loctite_乐泰胶_汉高乐泰授权(中国)总代理-鑫华良供应链 | 旋振筛_不锈钢旋振筛_气旋筛_旋振筛厂家—新乡市大汉振动机械有限公司 | 高铝砖-高铝耐火球-高铝耐火砖生产厂家-价格【荣盛耐材】 | 不锈钢酒柜|恒温酒柜|酒柜定制|酒窖定制-上海啸瑞实业有限公司 | 体视显微镜_荧光生物显微镜_显微镜报价-微仪光电生命科学显微镜有限公司 | 闪蒸干燥机-喷雾干燥机-带式干燥机-桨叶干燥机-[常州佳一干燥设备] | 广东恩亿梯电源有限公司【官网】_UPS不间断电源|EPS应急电源|模块化机房|电动汽车充电桩_UPS电源厂家(恩亿梯UPS电源,UPS不间断电源,不间断电源UPS) | 深圳市八百通智能技术有限公司官方网站 | 泰来华顿液氮罐,美国MVE液氮罐,自增压液氮罐,定制液氮生物容器,进口杜瓦瓶-上海京灿精密机械有限公司 | 农业四情_农业气象站_田间小型气象站_智慧农业气象站-山东风途物联网 | 权威废金属|废塑料|废纸|废铜|废钢价格|再生资源回收行情报价中心-中废网 | 专业生物有机肥造粒机,粉状有机肥生产线,槽式翻堆机厂家-郑州华之强重工科技有限公司 | 防火卷帘门价格-聊城一维工贸特级防火卷帘门厂家▲ | 定做大型恒温循环水浴槽-工业用不锈钢恒温水箱-大容量低温恒温水槽-常州精达仪器 | 沧州友城管业有限公司-内外涂塑钢管-大口径螺旋钢管-涂塑螺旋管-保温钢管生产厂家 | 精密模具制造,注塑加工,吹塑和吹瓶加工,EPS泡沫包装生产 - 济南兴田塑胶有限公司 | 【化妆品备案】进口化妆品备案流程-深圳美尚美化妆品有限公司 | 液压升降货梯_导轨式升降货梯厂家_升降货梯厂家-河南东圣升降设备有限公司 | ET3000双钳形接地电阻测试仪_ZSR10A直流_SXJS-IV智能_SX-9000全自动油介质损耗测试仪-上海康登 | 知网论文检测系统入口_论文查重免费查重_中国知网论文查询_学术不端检测系统 | 智慧钢琴-电钢琴-便携钢琴-数码钢琴-深圳市特伦斯乐器有限公司 | 英超直播_英超免费在线高清直播_英超视频在线观看无插件-24直播网 | 锡膏喷印机-全自动涂覆机厂家-全自动点胶机-视觉点胶机-深圳市博明智控科技有限公司 | 刺绳_刀片刺网_刺丝滚笼_不锈钢刺绳生产厂家_安平县浩荣金属丝网制品有限公司-安平县浩荣金属丝网制品有限公司 | 自动部分收集器,进口无油隔膜真空泵,SPME固相微萃取头-上海楚定分析仪器有限公司 | 天津拓展_天津团建_天津趣味运动会_天津活动策划公司-天津华天拓展培训中心 | 数年网路-免费在线工具您的在线工具箱-shuyear.com | 锯边机,自动锯边机,双面涂胶机-建业顺达机械有限公司 | 找培训机构_找学习课程_励普教育 | 高温热泵烘干机,高温烘干热泵,热水设备机组_正旭热泵 | 安徽合肥格力空调专卖店_格力中央空调_格力空调总经销公司代理-皖格制冷设备 | 生物颗粒燃烧机-生物质燃烧机-热风炉-生物颗粒蒸汽发生器-丽水市久凯能源设备有限公司 | 苗木价格-苗木批发-沭阳苗木基地-沭阳花木-长之鸿园林苗木场 | 齿轮减速电机一体机_蜗轮蜗杆减速马达-德国BOSERL齿轮减速机带电机生产厂家 | 大鼠骨髓内皮祖细胞-小鼠神经元-无锡欣润生物科技有限公司 | 制冷采购电子商务平台——制冷大市场| 塑料撕碎机_编织袋撕碎机_废纸撕碎机_生活垃圾撕碎机_废铁破碎机_河南鑫世昌机械制造有限公司 | 品牌设计_VI设计_电影海报设计_包装设计_LOGO设计-Bacross新越品牌顾问 | 网站制作优化_网站SEO推广解决方案-无锡首宸信息科技公司 |