samedi 15 février 2020

Random number generation in multiple objects of the same class

In this example code, I am creating 100 objects in a list with random x values, but the problem is that all of the created objects have the same x value. I tried searching web for solutions for this but I couldn't find any.

import random
list = []
class object:
    x = random.randint(1,1000)

for i in range(1,101):
    list.append(object())

for i in range(1,100):
    print(list[i].x)



Aucun commentaire:

Enregistrer un commentaire