lundi 5 novembre 2018

Randomly distribute numbers to lists

I have 4 empty lists. I would like to randomly distribute numbers starting from 1, until the sum of all the numbers in the lists exceeds 1000.

Here is the code I tried:

toplam=0
x=0
while toplam<1000:
    x+=1
    toplam+=x
a=[]
b=[]
c=[]
d=[]
list=[a,b,c,d]
z=[1,2,3,4]
import random
for i in range(x):
    t=random.choice(z)
    t2=list[t]
    t2.append(x)
print(list)

I want:

[[2, 6, 8, 18, 20, 21, 28, 40, 43], [1, 5, 19, 23, 24, 25, 29, 31, 33, 34, 35, 37, 39, 45], [9, 10, 11, 13, 15, 22, 26, 36, 38, 44], [3,
4, 7, 12, 14, 16, 17, 27, 30, 32, 41, 42]]

Sum of lists:

[186, 380, 224, 245]




Aucun commentaire:

Enregistrer un commentaire