简体   繁体   中英

How to generate correlated random numbers?

I want to generate two random integers in python, which are correlated. 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 . 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

So q is not in "the same range" as n . It is in the range [0, n] (or [0, n) , this is not clear from the question).

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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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