samedi 29 avril 2023

Random randint def() for storage variables

I'm trying to make a list where each time a random item from the list is chosen it gives the number to the define of r() which will take the number and give a random number between the values defined to the tenths place. The lists im using are for storage variables that i'm using for items for a game, though i feel there's a good chance i may want to modify these variables to tweak the stats of something, and I would like to try and make it easier to find the item and variables by having a short name of a define and the numbers such as r(1,2), instead of having ((random.randint(1x10,2x10)/10) for every item in the list. Although iv found whenever the same item is picked from the list it always gives the same output for the number, as an example if the list picks the item r(1,2) and runs through the define r() it might give a number of 1.3. The second time it picks the same item though instead of giving a different number it only gives the same number of 1.3

import random

def r(start,end):
    answ = (random.randint(start*10,end*10)/10)
    return answ

itemname =        ['name1','name2','name3','name4','name5']
itemvariable =    [r(1,2), r(1,3), r(3,5), r(1,6), r(6,7) ]

print(random.choice(itemname),random.choice(itemvariable))
print(random.choice(itemname),random.choice(itemvariable))
print(random.choice(itemname),random.choice(itemvariable))

example of current output(assuming random.choice() theoretically always picks first item of each list)

name1 1.3

name1 1.3

name1 1.3

example of desired output(assuming random.choice() theoretically always picks first item of each list)

name1 1.7

name1 1.2

name1 1.8

​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​




Aucun commentaire:

Enregistrer un commentaire