Question: I want to make a randomizer for the SPECIAL stats from Fallout New Vegas, i've built most of the code, but there's cases that the sum of the variables exceed/are below the cap of 40.

Is there a way to limit them, or in cases that the sum is below or over 40, distribute the difference?

```
strength = random.randint(1, 10)
perception = random.randint(1, 10)
endurance = random.randint(1, 10)
charisma = random.randint(1, 10)
intelligence = random.randint(1, 10)
agility = random.randint(1, 10)
luck = random.randint(1, 10)
sum = strength + perception + endurance + charisma + intelligence + agility + luck

diff = 40 - sum
if diff < 0:
    diff = (diff * - 1)

print("=======================")
print("Strength:", strength)
print("Perception:", perception)
print("Endurance:", endurance)
print("Charisma:", charisma)
print("Intelligence:", intelligence)
print("Agility:", agility)
print("Luck:", luck)
print("Total:", sum)
print("Difference:", diff)
print("=======================")

```

Answer: 
Instead of generating seven independent random numbers, generate seven numbers less than 40, and use their differences to generate your stats.

```
import random

STATMAX = 10

# generate six random numbers, sorted, to use as dividers in the range
rand_numbers = sorted(random.choices(range(40), k=6))
# calculate stats by taking the differences between them
stat_numbers = [(j - i) for (i, j) in zip([0] + rand_numbers, rand_numbers + [40])]
# for values higher than 10, dump the excess values into other stats
excess_points = sum(max(s - STATMAX, 0) for s in stat_numbers)
# also, drop stats above 10 before redistributing the points
stat_numbers = [min(s, STATMAX) for s in stat_numbers]
while excess_points > 0:
    idx = random.randint(0, len(stat_numbers) - 1)
    # this approach favors balanced stats by only adding one point at a time
    # an alternate approach would be to add as many points as possible, which
    # would favor imbalanced stats (i.e. minmaxing)
    if stat_numbers[idx] < STATMAX:
        stat_numbers[idx] += 1
        excess_points -= 1

strength, perception, endurance, charisma, intelligence, agility, luck = stat_numbers

```

You can tailor this approach in a few different ways. For example, if you want a chance of rolling less than 40 total stats, you could generate *seven* random numbers instead, and use the last random number as an endpoint instead of 40.
I would just generate the seven stats, and depending on the sum total, add or subtract randomly until the sum equals 40...

```
import random
stats = []

## Generate the stats
for x in range(7):
    stats.append(random.randint(1,10))

## If the sum total is 40, leave em.
if sum(stats) == 40:
    pass

## If it is less than 40, add +1's randomly until you reach 40.
elif sum(stats) < 40:
    while sum(stats) != 40:
        stat = random.randint(0,6)
        if stats[stat] != 10:
            stats[stat] = stats[stat] + 1

## If it is more than 40, subtract randomly until you reach 40.
elif sum(stats) > 40:
    while sum(stats) != 40:
        stat = random.randint(0,6)
        if stats[stat] != 1:
            stats[stat] = stats[stat] - 1

strength = stats[0]
perception = stats[1]
endurance = stats[2]
charisma = stats[3]
intelligence = stats[4]
agility = stats[5]
luck = stats[6]

```