New guy problems. I'm a Roleplaying game geek attempting to make a character builder. There are four attributes (Strength Agility Wits and Empathy). I'm attempting to assign numbers to each of these attributes randomly BUT you only get eight (8) total points to work with so you have to iteratively subtract them as their numbers are assigned. Here was my lame attempt. Didn't work. Do i need a For loop somewhere?
import random
some_number = random.randint(1,8)
num_one = 8 - random.randint(1,some_number)
num_two = num_one - (random.randint(1,num_one))
num_three = num_two - (random.randint(1,num_two))
num_four = num_three - (random.randint(1,num_three))
print(num_one)
print(num_two)
print(num_three)
print(num_four)
CodePudding user response:
An easier route is to distribute the 8 points randomly among the 4 stats:
import random
stats = [0,0,0,0]
for i in range(8):
stats[random.randrange(4)] = 1
print(stats)
A few runs:
[2, 3, 0, 3]
[1, 1, 4, 2]
[0, 2, 2, 4]
[2, 2, 2, 2]
[3, 1, 1, 3]
[0, 3, 4, 1]
[1, 2, 5, 0]
[4, 2, 2, 0]
[2, 3, 2, 1]
[3, 1, 3, 1]
If you want to get fancy with a class:
import random
class Player:
def __init__(self, name):
self.name = name
stats = [0,0,0,0]
for i in range(8):
stats[random.randrange(4)] = 1
self.str, self.agi, self.wit, self.emp = stats
def __repr__(self):
return f'Player(name={self.name!r}, ST={self.str}, AG={self.agi}, WT={self.wit}, EM={self.emp})'
players = [Player(name) for name in ('Mark','Joe')]
print(players)
A few runs:
[Player(name='Mark', ST=2, AG=3, WT=0, EM=3), Player(name='Joe', ST=2, AG=3, WT=0, EM=3)]
[Player(name='Mark', ST=1, AG=1, WT=4, EM=2), Player(name='Joe', ST=0, AG=2, WT=2, EM=4)]
[Player(name='Mark', ST=5, AG=1, WT=0, EM=2), Player(name='Joe', ST=3, AG=2, WT=2, EM=1)]
[Player(name='Mark', ST=3, AG=2, WT=3, EM=0), Player(name='Joe', ST=3, AG=3, WT=1, EM=1)]
[Player(name='Mark', ST=4, AG=2, WT=2, EM=0), Player(name='Joe', ST=3, AG=0, WT=2, EM=3)]
CodePudding user response:
There are a few good ways to go about this, but personally I'd use a list and a for loop.
This code starts off with all stats set to 0, and then loops 8 times, each time increasing a point for a random stat. After this code runs, stats[0]
represents Strength, stats[1]
represents Agility, etc.
import random
points = 8
stats = [0, 0, 0, 0]
for _ in range(points):
stats[random.randint(0,len(stats)-1)] = 1
print(stats)
CodePudding user response:
What you want to do is to use a loop:
import random
total_points = 8
remaining = total_points
values = []
for i in range(3):
value = remaining - random.randint(1, remaining)
remaining = remaining - value
values.append(value)
values.append(remaining)
This works by declaring a value called remaining
that we'll use to hold the number of points that can still be assigned. Then, for each iteration of the loop, we create a new value by subtracting a random number from remaining
. We then reassign remaining
with the value we calculated so we can ensure that it decreases with each successive calculation. Finally, we add the value we calculated to a list. At the end you'll have a list of four randomized values.
CodePudding user response:
Building on the accepted answer, you could even make a way to create random Players:
import random
random_name_list = ['Joe', 'Bob', 'Sam', 'Sally', 'Jerry', 'George', 'Tom']
class Player:
def __init__(self, name, pts=8):
self.name = name
stats = [0,0,0,0]
for i in range(pts):
stats[random.randrange(4)] = 1
self.str, self.agi, self.wit, self.emp = stats
def __repr__(self):
return f'Player(name={self.name!r}, ST={self.str}, AG={self.agi}, WT={self.wit}, EM={self.emp})'
@classmethod
def random_player(cls):
return Player(random.choice(random_name_list))
players = []
for _ in range(5):
players.append(Player.random_player())
print(players)
Output:
[Player(name='Sam', ST=0, AG=1, WT=3, EM=4),
Player(name='Tom', ST=3, AG=4, WT=0, EM=1),
Player(name='Sam', ST=3, AG=3, WT=0, EM=2),
Player(name='Joe', ST=2, AG=2, WT=1, EM=3),
Player(name='Tom', ST=1, AG=1, WT=3, EM=3)]