Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I select a random object from a class?

Tags:

python-3.x

I have a list of 100+ objects, and I'd like to select 5 random objects and store them into variables, but I am not sure how to do that. What to do? This is a simplified version of my code:

class playableCharacters:
    def __init__(self, name, power)

        self.name = name
        self.power = power

Aatrox = playableCharacters("Aatrox", 9)

Ahri = playableCharacters("Ahri", 3)
like image 973
Rad Razvan Avatar asked Jan 12 '19 12:01

Rad Razvan


People also ask

How do you select a random object in Java?

Here's how to pick a random one: int randomIndex = new Random(). nextInt(array. length); ObjectReturningSomething randomObject = objectArray.


3 Answers

You can create a static list, and push the object in that list on __init__, then you can get 5 randomized int and they can be used as an index to that static list.

Like this:

import random

class playableCharacters:
    instances = []

    def __init__(self, name, power):
        self.name = name
        self.power = power
        __class__.instances.append(self)

Aatrox = playableCharacters("Aatrox", 9)
Ahri = playableCharacters("Ahri", 3)
# 100 more instances declaration.

randIndex = random.randrange(len(playableCharacters.instances))
randPlayerCharacter = playableCharacters.instances[randIndex]

print(randPlayerCharacter.name)

Like this, you can get 4 more randomized instances.

like image 175
Letsintegreat Avatar answered Oct 04 '22 15:10

Letsintegreat


You could solve it with a class list

class playableCharacters:
    instances = []
    def __init__(self, name, power):

        self.name = name
        self.power = power
        __class__.instances.append(self)

a = playableCharacters("a", 9)
b = playableCharacters("b", 3)
c = playableCharacters("c", 9)
d = playableCharacters("d", 3)
# and so on.....

Now, you can get a set of 5 random object instances like

import random
random5 = random.choices(playableCharacters.instances, k=5)

I am not sure if this is the most optimal way if you have thousands of instances of the same class.

like image 28
najeem Avatar answered Oct 04 '22 17:10

najeem


How about you use random.choices(object_list, k=n) where n = number of random items:

import random

class playableCharacters:
    def __init__(self, name, power):
        self.name = name
        self.power = power

    # Displays name when printed
    def __repr__(self):
        return (self.name)

# 5 Objects created
Aatrox = playableCharacters("Aatrox", 9)

Ahri = playableCharacters("Ahri", 3)

Test = playableCharacters("Test", 5)

Test2 = playableCharacters("Test2", 1)

Test3 = playableCharacters("Test3", 5)

# You could automate this, but for now here is a list of objects
myCharacters = [Aatrox, Ahri, Test, Test2, Test3]

# K=5 gives a list of 5 random
randomObjects = random.choices(myCharacters, k=5)
print(randomObjects)

EDIT: Here is an automated version. Use a dictionary to represent the name and power key-value pairs:

import random

class playableCharacters:
    def __init__(self, name, power):
        self.name = name
        self.power = power

    # Displays name when printed
    def __repr__(self):
        return (self.name)

objects_to_be_created = {'Aatrox':9,'Ahri':3,'Test':5,'Test2':1,'Test3':5}

myCharacters = []

# Automatation
for o in objects_to_be_created:
    x = playableCharacters(o, objects_to_be_created[o])
    myCharacters.append(x)

# K=5 gives a list of 5 random
randomObjects = random.choices(myCharacters, k=5)
print(randomObjects)
like image 25
Suraj Kothari Avatar answered Oct 04 '22 17:10

Suraj Kothari