lundi 6 juin 2022

how to generate list of random numbers in python that sum of them have specific value

I want to generate n random numbers in increasing order between two numbers(n1 & n2) so that the sum of the randomly generated numbers would be the same as n2-n1

For example: If I generate 4 random number between 1 and 2, the results may be 0.15,0.20,0.31,0.34(addition is the same as 2-1=1)

In the similar way I want to generate 5 random numbers between 10 & 15,20 & 30,40 & 50 in loop

My script is

import random
import numpy as np
n=5
data=np.random.uniform(10,15,n)
print(data)

But it generates random numbers but the sum is not as per the condition mentioned in the question. Additionally if in the loop it can be done it's better. Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire