有没有办法将7个随机变量限制为40个?

2024-05-20 02:04:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我想为Fallout New Vegas的特殊统计数据创建一个随机化器,我已经构建了大部分代码,但是有些情况下变量的总和超过/低于上限40

有没有办法限制它们,或者如果总数低于或超过40,分配差额

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("=======================")

Tags: newdiffrandomstrengthintelligencesumprintrandint
3条回答

我不建议独立地生成属性(Green Clope Guy提供了一个关于如何更好地生成属性的指导性答案)

也就是说,如果出于某种原因,您仍然希望这样做,那么您可以按如下方式将差异分布到属性中:

import random

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)

list_attributes = [strength, perception, endurance, charisma, intelligence, agility, luck]
sum_attributes = sum(list_attributes)
diff = 40 - sum_attributes

if diff != 0:
    diff_partial = diff/len(list_attributes)
    for i, attribute in enumerate(list_attributes):
        list_attributes[i] = attribute + diff_partial

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

请记住,由于random返回float值,因此更新的属性也将是float。如果需要它们作为int,例如,可以在for循环中使用int()

不要生成七个独立的随机数,而是生成七个小于40的数字,并使用它们的差异生成统计数据

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

您可以用几种不同的方式来定制这种方法。例如,如果您希望滚动的总统计数据少于40个,则可以生成七个随机数,并使用最后一个随机数作为端点,而不是40个

如果您的目标是<=40,而不是直接瞄准40,一个选择要考虑的是确定超龄,并传播它跨越您的统计数字。用于应用超龄价差的数学/逻辑可以调整为不太苛刻,但感觉接近你所追求的

import random
import math

specialStats={"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=0
for stat in specialStats:
    sum += specialStats[stat]

#determine, if there is an overage, what the spread would be to subtract from stats
distributeOverage=0
if sum > 40: 
    distributeOverage = math.ceil((sum-40)/len(specialStats))

#apply difference from overage spread and print
print("=======================")
sum=0
for stat in specialStats:
    specialStats[stat] = specialStats[stat] -(distributeOverage * (specialStats[stat]>distributeOverage))
    sum += specialStats[stat] 
    print(stat+":", specialStats[stat])

print("Total:", sum)
print("=======================")

相关问题 更多 >