我想在python中生成两个相关的随机整数。
我们假设整数n
是0到10之间的随机整数,我希望整数q
在同一范围内,但不能超过n
。有什么建议?
from __future__ import division
from random import randint
population = randint(100, 1000)
number_of_successes = randint(1, 100)
number_of_fails = population-number_of_successes
probability = number_of_successes/population
答案 0 :(得分:1)
n整数是0到10之间的随机整数,我希望整数q在相同的范围内,但不能超过n
因此q
与n
不在“相同的范围内”。它位于[0, n]
(或[0, n)
范围内,问题不明确。)
这是一项非常简单的任务:
n = randint(100, 1000)
q = randint(100, n)
如果q
不能等于n
,请从n
范围内移除q
:
n = randint(100, 1000)
q = randint(100, n - 1)
答案 1 :(得分:1)
你可以试试这个:
import random
first_number = random.randint(0, 10)
second_number = random.randint(0, first_number)
答案 2 :(得分:0)
您可以将第一个变量用作第二个参数
from __future__ import division
from random import randint
population = randint(0 ,1000)
number_of_successes = randint(0 , population)
number_of_fails = population-number_of_successes
probability = number_of_successes/population