java的随机百分比 [英] java random percentages

查看:1450
本文介绍了java的随机百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要生成的 N 的百分比(0到100之间的整数),使得所有的总和的 N 的数字加起来为100

I need to generate n percentages (integers between 0 and 100) such that the sum of all n numbers adds up to 100.

如果我只是做 nextInt() N 的次,每次确保参数是100减去previously积累的总和,那么我的百分比是偏向(即第一个产生的数字通常是最大的等)。如何做到这一点的一个不带偏见的方式?

If I just do nextInt() n times, each time ensuring that the parameter is 100 minus the previously accumulated sum, then my percentages are biased (i.e. the first generated number will usually be largest etc.). How do I do this in an unbiased way?

推荐答案

一对夫妇的答案的建议采摘随机%,而且考虑它们之间的差异。正如尼基塔韦柏康指出,这不会给均匀分布在所有的可能性;特别是,零将较不频繁比预期的

A couple of answers suggest picking random percents and taking the differences between them. As Nikita Ryback points out, this will not give the uniform distribution over all possibilities; in particular, zeroes will be less frequent than expected.

要解决这个问题,想先从100百分比和插入分隔的。我将展示一个例子,10:

To fix this, think of starting with 100 'percents' and inserting dividers. I will show an example with 10:

 % % % % % % % % % % 

有11位,我们可以插入一个除法:任何两个百分比之间,或在开始或结束。因此,插一句:

There are eleven places we could insert a divider: between any two percents or at the beginning or end. So insert one:

 % % % % / % % % % % % 

这再presents选择四和六。现在插入另一个分。这一次,有12处,因为已经插入分隔创建和多余的。特别是,有两种方法来获得

This represents choosing four and six. Now insert another divider. This time, there are twelve places, because the divider already inserted creates and extra one. In particular, there are two ways to get

 % % % % / / % % % % % % 

之前或previous分后插入。直到你有尽可能多的分隔,因为你需要,你可以继续处理(除百分比的数量少一个。)

either inserting before or after the previous divider. You can continue the process until you have as many dividers as you need (one fewer than the number of percents.)

 % % / % / % / / % % % / % % % / 

这相当于2,1,1,0,3,3,0。

This corresponds to 2,1,1,0,3,3,0.

我们可以证明这给均匀分布。 100组合物的数量为K为二项式系数100 + K-1选择K-1。那是 (100 + K-1)的(100 + K-2) ... 101 /(K-1)的(K-2)* ... * 2 * 1 从而选择任何特定的组合物的概率是这种倒数。当我们插入分隔一次,首先我们从101的位置,然后102,103等选择,直到我们到达100 + K-1。这样插入的任何特定序列的概率为1 /(100 + K-1)* ... * 101。多少插入序列产生相同的组合物?最终组合物含有k-1个分频器。他们可能已被插入以任何顺序,所以有(K-1)! ,让序列上升到一个给定的成分。因此,任何特定组合物的概率是什么,应该

We can prove that this gives the uniform distribution. The number of compositions of 100 into k parts is the binomial coefficient 100+k-1 choose k-1. That is (100+k-1)(100+k-2)...101 / (k-1)(k-2)*...*2*1 Thus the probability of choosing any particular composition is the reciprocal of this. As we insert dividers one at a time, first we choose from 101 positions, then 102, 103, etc until we get to 100+k-1. So the probability of any particular sequence of insertions is 1 / (100+k-1)*...*101. How many insertion sequences give rise to the same composition? The final composition contains k-1 dividers. They could have been inserted in any order, so there are (k-1)! sequences that give rise to a given composition. So the probability of any particular composition is exactly what it should be.

在实际的code,你可能不会再present你的步骤是这样的。您应该能够牵住上的数字,而不是百分比和除法序列。我还没有想过这个算法的复杂度。

In actual code, you probably wouldn't represent your steps like this. You should be able to just hold on to numbers, rather than sequences of percents and dividers. I haven't thought about the complexity of this algorithm.

这篇关于java的随机百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆