I'm trying to get random numbers to reach a certain sum I'm using for a DND type game. Characters can have 2-6 points to use, and they fill it with modules worth 1, 2, or 3 points.
So if a character had 6 points, they could have it any sequence as long as it sums to 6. If 2 points, they could either have two 1point modules or one two point module.
For this, I'm trying a random generator to do that. To start with a random number, then subtract that number from the maximum points, and then generate a new random number under that new maximum until it reaches 0. Here's my code, and unfortunately it often generates sums one point above the limit. Any advice? Thank you.
tp = totalPoint
oneMods, twoMods, threeMods = 0, 0, 0
while tp > 0:
if(tp >= 3):
var = random.randint(1, 3)
tp = tp - var
if var == 3:
threeMods +=1
elif var == 2:
twoMods += 1
else:
oneMods += 1
elif(tp <=2):
var = random.randint(1,2)
tp = tp - var
if var == 2:
twoMods +=1
else:
oneMods += 1
else:
var = 1
tp = tp -1
oneMods +=1
await ctx.send(f'You have:\nLevel one modules: {oneMods}\nLevel 2 modules: {twoMods}\nLevel three mods: {threeMods}')
Aucun commentaire:
Enregistrer un commentaire