我想为Fallout新维加斯的特殊统计数据做一个随机化,我已经构建了大部分代码,但是在某些情况下,变量之和超过/低于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("=======================")发布于 2021-05-26 20:50:24
不要生成七个独立的随机数,而是生成小于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个,则可以生成7个随机数,并将最后一个随机数用作端点而不是40。
发布于 2021-05-26 21:17:22
我不建议独立地生成属性(绿斗篷盖伊给出了一个关于如何做得更好的有益回答)。
尽管如此,如果出于某种原因,您仍然想要这样做,那么您可以将这些差异分布在以下属性上:
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()。
发布于 2021-05-26 21:15:36
如果您的目标是<=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("=======================")https://stackoverflow.com/questions/67712245
复制相似问题