[英]Using random.randint() to generate series of numbers that equals an input
我正在尝试使用以下 function:
random.randint()
生成一系列应该等于 input() integer 的数字。 这里唯一的问题是,我尝试通过“while if”循环来做到这一点。 其中循环不断生成数字,直到随机组合等于 n = int(input())。 但是,这种方法非常耗时,有时 pycharm 需要一整分钟才能找到正确的组合。 Any1 知道是否已经有解决此问题的功能?
编辑:将按照 ppl 的要求添加代码,但是是的,代码是变量的巨大 bcuz。 范围来自 (1, a) 的原因是因为 (0, a) 的范围会导致随机模块出错。 如果您有兴趣,请注意,我尝试了相同的代码块,但使用其他变量来生成不同的东西,例如范围为 (0, a) 的损益表,它工作得很好,我不知道为什么范围 ( 0, a) 有时有效,有时无效。
# Cash
ca = 0
# Marketable securities
ms = 0
# Accounts receivable
ar = 0
# Inventories
i = 0
# Total current assets
tca = 0
# Land and buildings
lab = 0
# Machinery and equipment
mae = 0
# Furniture and fixtures
faf = 0
# Vehicles
v = 0
# Other (includes financial leases)
ot = 0
# Total gross fixed assets
tgfa = 0
# Less: Accumulated depreciation
ad = 0
# Net fixed assets
nfa = 0
# Total assets
ta = 0
# Accounts payable
ap = 0
# Notes payable
npp = 0
# Accruals
acc = 0
# Total current liabilities
tcl = 0
# Long-term debt (includes financial leases)
ltd = 0
# Total liabilities
tl = 0
# Preferred stock
ps = 0
# Common stock
cs = 0
# Paid-in capital in excess of par on common stock
pic = 0
# Retained earnings
re = 0
# Total stockholders’ equity
tse = 0
# Total liabilities and stockholders’ equity
tlase = 0
while ta <= 0 and 0 >= tlase:
ca += random.randint(1,a)
ms += secrets.randbelow(ca)
ar += secrets.randbelow(ca)
i += secrets.randbelow(ca)
lab += random.randint(1,a)
mae += random.randint(1,a)
faf += secrets.randbelow(ca)
v += secrets.randbelow(ca)
ot += secrets.randbelow(ca)
ad += secrets.randbelow(ca)
ap += secrets.randbelow(ca)
npp += secrets.randbelow(ca)
acc += secrets.randbelow(ca)
ltd += random.randint(1,a)
ps += secrets.randbelow(ca)
cs += secrets.randbelow(ca)
pic += secrets.randbelow(ca)
re += random.randint(1,a)
tca += ca + ms + ar + i
tgfa += lab + mae + faf + v + ot
nfa += tgfa - ad
ta += tgfa - (ad + ca + ms + ar + i)
tcl += ap + npp + acc
tl += tcl + ltd
tse += ps + cs + pic + re
tlase += ps + cs + pic + re + tcl + ltd
if 0 >= ta and tlase <= 0:
ca *= 0
ms *= 0
ar *= 0
i *= 0
lab *= 0
mae *= 0
faf *= 0
v *= 0
ot *= 0
ad *= 0
ap *= 0
npp *= 0
acc *= 0
ltd *= 0
ps *= 0
cs *= 0
pic *= 0
re *= 0
tca *= 0
tgfa *= 0
nfa *= 0
ta *= 0
tcl *= 0
tl *= 0
tse *= 0
tlase *= 0
elif ta != tlase:
ca *= 0
ms *= 0
ar *= 0
i *= 0
lab *= 0
mae *= 0
faf *= 0
v *= 0
ot *= 0
ad *= 0
ap *= 0
npp *= 0
acc *= 0
ltd *= 0
ps *= 0
cs *= 0
pic *= 0
re *= 0
tca *= 0
tgfa *= 0
nfa *= 0
ta *= 0
tcl *= 0
tl *= 0
tse *= 0
tlase *= 0
else:
print("true")
如果我理解叙述和评论,您的程序将提示输入 integer 值,并尝试找到固定数量的整数,当它们相加时将等于输入的数字。 有了这个,这里有一个可能会刺激你的代码片段。
import random
while True:
response = input("Enter a number or enter 'Q' to quit: ")
if response.upper() == "Q":
break
num = int(input("Enter the number of integers that need to add up to your value: "))
summary = int(response)
tries = 0
while True:
work = 0
x = []
y = 0
for i in range (0, num):
y = random.randint(1, summary)
x.append(y)
work += y
tries += 1
if (work == summary):
break
print ("The selected random values are:", x, "after", tries, "tries")
我扔了一个计数器,说明程序尝试了多少次整数组合来求和输入的值。
以下是在我的终端上运行的测试。
@Una:~/Python_Programs/RandomSum$ python3 RandomSum.py
Enter a number or enter 'Q' to quit: 442
Enter the number of integers that need to add up to your value: 4
The selected random values are: [38, 119, 124, 161] after 6476 tries
Enter a number or enter 'Q' to quit: 100
Enter the number of integers that need to add up to your value: 3
The selected random values are: [10, 46, 44] after 1392 tries
Enter a number or enter 'Q' to quit: 442
Enter the number of integers that need to add up to your value: 6
The selected random values are: [39, 236, 8, 30, 65, 64] after 49632 tries
Enter a number or enter 'Q' to quit: Q
试试看。
import random
def constrained_sum_sample_pos(n, total):
"""Return a randomly chosen list of n positive integers summing to total.
Each such list is equally likely to occur."""
dividers = sorted(random.sample(range(1, total), n - 1))
return [a - b for a, b in zip(dividers + [total], [0] + dividers)]
这段代码对我来说很好用,它不是我真正想要的,但它可以完成工作。 唯一的区别在于此代码中,您输入最大值,生成的数字总和等于您输入的最大值。 但是,对于我的代码,我试图让代码本身从范围中生成一个随机最大值,并且其他生成的数字的总和应该仍然等于它。 因此,唯一的区别是我希望使最大值与它的变量一起随机生成,以使生成的 output 完全随机,尽管每次都输入相同的输入。
对于那些仍然没有明白我的意思的人:提供的代码是这样的:A:你输入这个值 B + C + D(随机生成)= A
对于我的代码:您的输入 = 输入() A:python 从范围(1,您的输入)B + C + D(也是随机生成)= A 即使您使用相同的输入,每次随机生成的数字的总和将等于从范围中随机生成的输入
我希望我没有过度解释
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.