产生N个随机数在给定范围是总结到一个给定的总和

产生N个随机数在给定范围是总结到一个给定的总和

本文介绍了产生N个随机数在给定范围是总结到一个给定的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一次来这里,在#1。我希望有人能帮助我与我的搜索算法的。

first time here at Stackoverflow. I hope someone can help me with my search of an algorithm.

我需要生成N个随机数在给定的范围即相加为一个给定的总和!

I need to generate N random numbers in given Ranges that sum up to a given sum!

例如:Generatare 3个数字的总和为11

For example: Generatare 3 Numbers that sum up to 11.

范围:

  1. 在1和3之间的价值。
  2. 在5至8值。
  3. 7之间3和值。

的生成号码这好例子可以是:2,5,4

The Generated numbers for this examle could be: 2, 5, 4.

我已经搜索了很多,无法找到我需要的解决方案。

I already searched alot and couldnt find the solution i need.

有可能产生像恒定总和unsing模这样N个编号:generate随机数,其中的总和是恒定的但我不能得到与范围进行。

It is possible to generate like N Numbers of a constant sum unsing modulo like this:generate random numbers of which the sum is constantBut i couldnt get that done with ranges.

或者通过产生N个随机的值,总结起来,然后通过随机总和除以常数之和之后与商数的建议。

Or by generating N random values, sum them up and then divide the constant sum by the random sum and afterwards multiplying each random number with that quotient as proposed here.

主要问题,为什么我不能采取的解决方案是,每一个我的随机值都有不同的范围和我需要的值withing范围(没有频率occurances在最小/最大为例,它发生,如果我切均匀分布关闭这是不太/大于最小值/最大值)的值。

Main Problem, why i cant adopt those solution is that every of my random values has different ranges and i need the values to be uniformly distributed withing the ranges (no frequency occurances at min/max for example, which happens if i cut off the values which are less/greater than min/max).

我也想到了soultion,取一个随机数(该实施例中,值1,2或3),生成的范围内的值(最小/最大或最小和的总和上的其余部分,这取决于之间这是小),减去这个数字我给总和,并保持了下去,直到一切都分布。但是,这将是可怕的inefficiant。我真的可以使用的方式,其中的算法的运行时间是固定的。

I also thought of an soultion, taking a random number (in that Example, Value 1,2 or 3), generate the value within the range (either between min/max or min and the rest of the sum, depending on which is smaller), substracting that number of my given sum, and keep that going until everything is distributed. But that would be horrible inefficiant. I could really use a way where the runtime of the algorithm is fixed.

我试图让运行在Java中。但是,信息不是importend,但如果有人已经有了一个解决方案做好了准备。我需要的是一个描述或算法和想法。

I'm trying to get that running in Java. But that Info is not that importend, except if someone already has a solution ready. All i need is a description or and idea of an algorithm.

推荐答案

首先,请注意,这个问题是等价于:

First, note that the problem is equivalent to:

生成K个的款项数目为y,使得X_1,...,x_k -  每个人都有一个限度。

第二可通过简单地减小下限从数量来实现 - 所以在exmple,它相当于:

The second can be achieved by simply reducing the lower bound from the number - so in your exmple, it is equivalent to:

生成3个数字,使得X1< = 2; ×2&其中; = 3; ×3其中; = 4; X1 + X2 + X3 = 2

请注意,该第二问题可以以各种方式来解决,其中之一是:

Note that the 2nd problem can be solved in various ways, one of them is:

生成与 h_i 列表每个元素重复 - 其中 h_i 是限制元素 - 洗牌的名单,并选择第一要素

generate a list with h_i repeats per element - where h_i is the limit for element i - shuffle the list, and pick the first elements.

在您的例子,该列表是: [X1,X1,X2,X2,X2,X3,X3,X3,X3的] - 洗牌并选择前两个元素。

In your example, the list is: [x1,x1,x2,x2,x2,x3,x3,x3,x3] - shuffle it and choose first two elements.

(*)请注意,洗牌的列表可以使用费雪耶茨的算法来完成。 (你可以中止算法在中间,你通过了所需的限制后)。

(*) Note that shuffling the list can be done using fisher-yates algorithm. (you can abort the algorithm in the middle after you passed the desired limit).

这篇关于产生N个随机数在给定范围是总结到一个给定的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 16:27