简体   繁体   English

如何生成相关的随机数?

[英]How to generate correlated random numbers?

I want to generate two random integers in python, which are correlated. 我想在python中生成两个相关的随机整数。 Let's say that integer n is a random integer between 0 and 10, and I want integer q to be in the same range but be not more that n . 假设整数n是0到10之间的随机整数,并且我希望整数q处于相同范围内,但不大于n Any suggestions? 有什么建议么?

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

n integer is a random integer between 0 and 10, and i want integer q to be in the same range but be not more that n n整数是0到10之间的随机整数,我希望整数q处于相同范围内,但不超过n

So q is not in "the same range" as n . 因此, qn不在“相同范围内”。 It is in the range [0, n] (or [0, n) , this is not clear from the question). 它在[0, n] (或[0, n) ,从问题中尚不清楚)。

This is quite a simple task: 这是一个非常简单的任务:

n = randint(100, 1000)
q = randint(100, n)

If q must not be equal to n , remove n from the range of q : 如果q一定不等于n ,则从q范围中删除n

n = randint(100, 1000)
q = randint(100, n - 1)

You can try this: 您可以尝试以下方法:

import random

first_number = random.randint(0, 10)

second_number = random.randint(0, first_number)

You can use the first variable as the second parameter 您可以将第一个变量用作第二个参数

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

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

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