我看过其他类似问题的帖子。我知道如何生成N个正整数。我也知道如何限制随机生成的整数的总和。唯一的问题是满足N个值均不超出指定范围的条件。
例如generate_ints(n, total, low, high)
应生成n个值数组,以使每个值介于低和高之间,并且总和为总数。任何指针/帮助将不胜感激。
例如generate_ints(4, 40, 4, 15)
应该生成类似
[7,10,13,10]
我不在乎数字是否重复,只要它们不高度偏斜即可。我正在使用
np.randon.randint(5,15,n)
选择整数。到目前为止,我已经尝试了以下方法,但是它不起作用-
import numpy as np
import random
from random import uniform as rand
total=50
n=10
low=2
high=15
result=[]
m=0
nobs=1
while nobs <= n:
if m >= (total - low):
last_num= total -new_tot
result.append(last_num)
else:
next_num=np.random.randint(low,high,1)
new_tot = sum(result) + next_num
result.append(next_num)
m=new_tot
nobs +=1
print result
print sum(result)
再次感谢。
最佳答案
import numpy as np
def sampler(samples, sum_to , range_list):
assert range_list[0]<range_list[1], "Range should be a list, the first element of which is smaller than the second"
arr = np.random.rand(samples)
sum_arr = sum(arr)
new_arr = np.array([int((item/sum_arr)*sum_to) if (int((item/sum_arr)*sum_to)>range_list[0]and int((item/sum_arr)*sum_to)<range_list[1]) \
else np.random.choice(range(range_list[0],range_list[1]+1)) for item in arr])
difference = sum(new_arr) - sum_to
while difference != 0:
if difference < 0 :
for idx in np.random.choice(range(len(new_arr)),abs(difference)):
if new_arr[idx] != range_list[1] :
new_arr[idx] += 1
if difference > 0:
for idx in np.random.choice(range(len(new_arr)), abs(difference)):
if new_arr[idx] != 0 and new_arr[idx] != range_list[0] :
new_arr[idx] -= 1
difference = sum(new_arr) - sum_to
return new_arr
new_arr = sampler (2872,30000,[5,15])
print "Generated random array is :"
print new_arr
print "Length of array:", len(new_arr)
print "Max of array: ", max(new_arr)
print "min of array: ", min(new_arr)
print "and it sums up to %d" %sum(new_arr)
结果 :
Generated random array is :
[ 9 10 9 ..., 6 15 11]
Length of array: 2872
Max of array: 15
min of array: 5
and it sums up to 30000