繁体   English   中英

如何生成一定数量的随机整数,它们加起来等于一定数量并且至少是一定数量或更多?

[英]How to generate a certain number of random whole numbers that add up to a certain number and are at least a certain number or more?

我想生成 10 个整数,加起来为 40,并且在 2-6 的范围内。

例如:

2 + 6 + 2 + 5 + 6 + 2 + 2 + 6 + 3 + 6 = 40

2 到 6 之间的 10 个随机数加起来为 40。

鉴于相对较小的搜索空间,您可以使用itertools.combinations_with_replacement()生成 2 到 6 之间的 10 个数字的所有可能序列,保存总和为 40 的序列 - 然后在请求时随机选择并随机播放一个:

from itertools import combinations_with_replacement as combine
from random import choice, shuffle

sequences = [list(combo) for combo in combine(range(2, 6+1), 10) if sum(combo) == 40]

def get_random_sequence_of_sum_40():
    seq = choice(sequences)
    shuffle(seq)
    return seq

# ... later when you need random sequences of sum=40
for i in range(10):
    rand_sequence = get_random_sequence_of_sum_40()
    print(f"The sum of {rand_sequence} is {sum(rand_sequence)}")

样品 output:

The sum of [6, 3, 4, 4, 3, 3, 4, 6, 5, 2] is 40
The sum of [3, 3, 5, 3, 5, 5, 3, 3, 5, 5] is 40
The sum of [3, 3, 6, 3, 4, 6, 3, 4, 4, 4] is 40
The sum of [6, 6, 5, 3, 4, 3, 3, 2, 4, 4] is 40
The sum of [5, 2, 2, 4, 4, 4, 5, 4, 4, 6] is 40
The sum of [4, 4, 4, 3, 4, 4, 3, 6, 4, 4] is 40
The sum of [4, 4, 5, 4, 2, 4, 4, 5, 5, 3] is 40
The sum of [4, 2, 6, 2, 5, 6, 2, 5, 4, 4] is 40
The sum of [3, 6, 3, 4, 3, 3, 4, 4, 6, 4] is 40
The sum of [2, 2, 6, 2, 3, 5, 6, 4, 4, 6] is 40

如果我理解你的问题是正确的,那么应该这样做:

import random as rd

run = True
while run:
    list = []
    for i in range(10):
        ran_num = rd.randint(2, 6)
        list.append(ran_num)
        if sum(list) >= 40 and len(list) == 10:
            print(list)
            run = False  

这将打印出 10 个数字的列表,这些数字加起来为 40 或更多。

我的想法是生成 [2,6] 范围内的数字,直到列表的长度为 10,然后开始检查总和,如果不是 40,则删除列表的第一个元素并生成一个新数字。 唯一的问题是您可能需要检查是否可以将这些数字相加到您的目标数字,例如,如果它是奇数但所有生成的数字都是偶数,则它永远无法达到目标数字。

import random

low,high = 2,6
count = 10
target = 40

k = []
r = range(low,high+1)
tries = 0
while True:
    k.append(random.choice(r))
    if len(k) == count:
        if sum(k) == target:
            break
        k = k[1:]
        tries += 1
        
print(k)
print(len(k))
print(sum(k))
print(tries)

我要说的主要问题是如何定义数字的随机性。

import random

wanted_sum = 40
number_of_numbers = 10

min_number = 2
max_number = 6

if number_of_numbers * min_number > wanted_sum or number_of_numbers * max_number < wanted_sum:
    print("not possible")
else:
    to_distribute = wanted_sum - min_number * number_of_numbers
    
    print_list = [min_number for i in range(number_of_numbers)]
    while True:
        for i in range(number_of_numbers):
            if print_list[i] < max_number:
                if random.uniform(0, 1) >= 0.5:
                    print_list[i] += 1
                    to_distribute -= 1
            if to_distribute == 0:
                break
        if to_distribute == 0:
            break
    print(print_list, sum(print_list))

在这里我想再带来一件事,如果你只是重复范围值直到大小。 像 2,3,4..6 并再次从 2,3.. 开始,直到列表大小为 10。

对于这个问题,我们也可以得到答案。

l = [*range(2, 6+1, 1)]
for i in range(0, 10-len(l)):
    if l[-1] == 6: 
        l.insert(len(l), 2)
    else:
        l.insert(len(l), l[-1]+1)

Output:

l
[2, 3, 4, 5, 6, 2, 3, 4, 5, 6]

sum(l)
40

我也对范围 2-6 列表大小 15 应用相同的规则,我想要答案 60。

我知道我是初学者,我的答案不是一个完美的答案,但代码确实有效:

from random import randint
result = 0
while result != 40:
    a, b, c, d, e = randint(2,6), randint(2,6), randint(2,6), randint(2,6), randint(2,6)
    f, g, h, i, j = randint(2,6), randint(2,6), randint(2,6), randint(2,6), randint(2,6)
    result = a+b+c+d+e+f+g+h+i+j
a,b,c,d,e,f,g,h,i,j,x = str(a), str(b), str(c), str(d), str(e), str(f), str(g), str(h), str(i), str(j), '+'
print(a,x,b,x,c,x,d,x,e,x,f,x,g,x,h,x,i,x,j,x,'=',result)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM