Counter()返回看似随机的大整数

2024-06-30 13:15:12 发布

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

请原谅这个庞大的代码块,但我根本无法找出哪里出了问题。据我所知这应该管用。焊接清单是随机分配的设备,然后所有匹配值的总和。但出于某种原因,它返回的是几乎随机的大数。我真的需要帮助。你知道吗

import random
from collections import Counter

def getRandomWeightedElement(**data):
    rand = random.randint(1, sum(data.values()))

    for key, value in data.items():
        rand -= value
        if rand <= 0:
            return key

equipment = {
"nothing" : Counter({"physA":0}),
"woodenShield" : Counter({"physD":1,"fireD":3}),

#Physical:

#Bronze Weapons
"bronzeDag" : Counter({"physA":12}),
"bronzeSword" : Counter({"physA":23,"physD":2}),
"bronzeBAxe" : Counter({"physA":23}),
"bronze2HSword" : Counter({"physA":26}),
"bronzeMace" : Counter({"physA":26}),
"bronzeWarHammer" : Counter({"physA":26}),

#Iron Weapons
"ironDag" : Counter({"physA":15}),
"ironSword" : Counter({"physA":27,"physD":3}),
"ironBAxe" : Counter({"physA":27}),
"iron2HSword" : Counter({"physA":31}),
"ironMace" : Counter({"physA":31}),
"ironWarHammer" : Counter({"physA":31}),

#Bronze Armor
"bronzeShield" : Counter({"physD":10,"rangD":15,"magD":5}),
"bronzeMedHelm" : Counter({"physD":2,"rangD":4,"magD":1}),
"bronzeFullHelm" : Counter({"physD":4,"rangD":6,"magD":2}),
"bronzeChainbody" : Counter({"physD":6,"rangD":9,"magD":3}),
"bronzePlatebody" : Counter({"physD":7,"rangD":12,"magD":3}),
"bronzeChainlegs" : Counter({"physD":4,"rangD":6,"magD":2}),
"bronzePlatelegs" : Counter({"physD":5,"rangD":8,"magD":2}),
"bronzeBoots" : Counter({"physD":2,"rangD":3,"magD":1}),

#Iron Armor
"ironShield" : Counter({"physD":12,"rangD":18,"magD":6}),
"ironMedHelm" : Counter({"physD":3,"rangD":5,"magD":1}),
"ironFullHelm" : Counter({"physD":4,"rangD":8,"magD":2}),
"ironChainbody" : Counter({"physD":7,"rangD":11,"magD":3}),
"ironPlatebody" : Counter({"physD":9,"rangD":14,"magD":4}),
"ironChainlegs" : Counter({"physD":4,"rangD":8,"magD":2}),
"ironPlatelegs" : Counter({"physD":6,"rangD":9,"magD":3}),
"ironBoots" : Counter({"physD":2,"rangD":4,"magD":1}),

"pineSBow" : Counter({"rangA":22}),
"bronzeNecklace" : Counter({"physD":1}),
"bronzeRing" : Counter({"rangA":0}),
"silverRing" : Counter({"rangA":0})
}

def goblinGen():
    "Generates a Goblin, ready to fight"

    mainH = getRandomWeightedElement(**{"bronzeDag":40,"pineSBow":25,"bronzeSword":15,"bronzeBAxe":10,"ironSword":10})
    offH = getRandomWeightedElement(**{"nothing":50,"woodenShield":40,"bronzeShield":10})
    head = getRandomWeightedElement(**{"nothing":80,"bronzeMedHelm":15,"ironMedHelm":5})
    neck = getRandomWeightedElement(**{"nothing":90,"bronzeNecklace":10})
    chest = getRandomWeightedElement(**{"nothing":60,"bronzeChainbody":40})
    legs = getRandomWeightedElement(**{"nothing":75,"bronzeChainlegs":20,"ironChainlegs":5})
    gloves = getRandomWeightedElement(**{"nothing":95,"leatherGloves":5})
    boots = getRandomWeightedElement(**{"nothing":95,"bronzeBoots":5})
    ring1 = getRandomWeightedElement(**{"nothing":95,"bronzeRing":5})
    ring2 = getRandomWeightedElement(**{"nothing":95,"silverRing":5})

    wielding = [mainH,offH,head,neck,chest,legs,gloves,boots,ring1,ring2]
    print("\n",wielding,"\n")

    total = equipment["nothing"]

    for item in wielding:
       total += equipment[item]

    print("Physical Attack:",total["physA"],"Physical Defence:",total["physD"])
    print("Ranged Attack:", total["rangA"],"Ranged Defence:", total["rangD"])
    print("Magic Attack:",total["magA"],"Magic Defence:",total["magD"])
    print("Fire Attack Damage",total["fireAD"],"Fire Defence Protection",total["fireDP"])
    print("Poison Attack Chance",total["poisAC"],"Poison Defence Chance",total["poisDC"])
    print("Prayer Offence Bonus",total["prayOB"],"Prayer Defence Bonus",total["prayDB"])

goblinGen()

Tags: datacountertotalprintequipmentattacknothingrand
1条回答
网友
1楼 · 发布于 2024-06-30 13:15:12

所以基本的问题是你对nothing计数器进行了变异,所以当你生成几个li'l gobbos时,结果是累积的。这是通过实例化一个新的总计数计数器来修复的。你知道吗

我还修复了其他一些东西:

  • 设备信息不再是全部的,因为这是不必要的。你知道吗
  • 您不需要在getRandomWeightedElement中使用**字典解包语法。你知道吗
  • 地精选择的装备现在存储在一个dict中,所以你一眼就能看到哪个插槽是哪个插槽。(pprint.pprint用于漂亮的打印。)

import random
from collections import Counter
from pprint import pprint

def getRandomWeightedElement(data):
    rand = random.randint(1, sum(data.values()))

    for key, value in data.items():
        rand -= value
        if rand <= 0:
            return key


equipment = {
    "nothing": {"physA": 0},
    "woodenShield": {"physD": 1, "fireD": 3},

    # Physical:

    # Bronze Weapons
    "bronzeDag": {"physA": 12},
    "bronzeSword": {"physA": 23, "physD": 2},
    "bronzeBAxe": {"physA": 23},
    "bronze2HSword": {"physA": 26},
    "bronzeMace": {"physA": 26},
    "bronzeWarHammer": {"physA": 26},

    # Iron Weapons
    "ironDag": {"physA": 15},
    "ironSword": {"physA": 27, "physD": 3},
    "ironBAxe": {"physA": 27},
    "iron2HSword": {"physA": 31},
    "ironMace": {"physA": 31},
    "ironWarHammer": {"physA": 31},

    # Bronze Armor
    "bronzeShield": {"physD": 10, "rangD": 15, "magD": 5},
    "bronzeMedHelm": {"physD": 2, "rangD": 4, "magD": 1},
    "bronzeFullHelm": {"physD": 4, "rangD": 6, "magD": 2},
    "bronzeChainbody": {"physD": 6, "rangD": 9, "magD": 3},
    "bronzePlatebody": {"physD": 7, "rangD": 12, "magD": 3},
    "bronzeChainlegs": {"physD": 4, "rangD": 6, "magD": 2},
    "bronzePlatelegs": {"physD": 5, "rangD": 8, "magD": 2},
    "bronzeBoots": {"physD": 2, "rangD": 3, "magD": 1},

    # Iron Armor
    "ironShield": {"physD": 12, "rangD": 18, "magD": 6},
    "ironMedHelm": {"physD": 3, "rangD": 5, "magD": 1},
    "ironFullHelm": {"physD": 4, "rangD": 8, "magD": 2},
    "ironChainbody": {"physD": 7, "rangD": 11, "magD": 3},
    "ironPlatebody": {"physD": 9, "rangD": 14, "magD": 4},
    "ironChainlegs": {"physD": 4, "rangD": 8, "magD": 2},
    "ironPlatelegs": {"physD": 6, "rangD": 9, "magD": 3},
    "ironBoots": {"physD": 2, "rangD": 4, "magD": 1},

    "pineSBow": {"rangA": 22},
    "bronzeNecklace": {"physD": 1},
    "bronzeRing": {"rangA": 0},
    "silverRing": {"rangA": 0},
}


def goblinGen():
    "Generates a Goblin, ready to fight"

    wielding = {
        'mainH': getRandomWeightedElement({"bronzeDag": 40, "pineSBow": 25, "bronzeSword": 15, "bronzeBAxe": 10, "ironSword": 10}),
        'offH': getRandomWeightedElement({"nothing": 50, "woodenShield": 40, "bronzeShield": 10}),
        'head': getRandomWeightedElement({"nothing": 80, "bronzeMedHelm": 15, "ironMedHelm": 5}),
        'neck': getRandomWeightedElement({"nothing": 90, "bronzeNecklace": 10}),
        'chest': getRandomWeightedElement({"nothing": 60, "bronzeChainbody": 40}),
        'legs': getRandomWeightedElement({"nothing": 75, "bronzeChainlegs": 20, "ironChainlegs": 5}),
        'gloves': getRandomWeightedElement({"nothing": 95, "leatherGloves": 5}),
        'boots': getRandomWeightedElement({"nothing": 95, "bronzeBoots": 5}),
        'ring1': getRandomWeightedElement({"nothing": 95, "bronzeRing": 5}),
        'ring2': getRandomWeightedElement({"nothing": 95, "silverRing": 5}),
    }
    pprint(wielding)

    total = Counter()
    for item in wielding.values():
        total += equipment[item]

    print("Physical Attack:", total["physA"], "Physical Defence:", total["physD"])
    print("Ranged Attack:", total["rangA"], "Ranged Defence:", total["rangD"])
    print("Magic Attack:", total["magA"], "Magic Defence:", total["magD"])
    print("Fire Attack Damage", total["fireAD"], "Fire Defence Protection", total["fireDP"])
    print("Poison Attack Chance", total["poisAC"], "Poison Defence Chance", total["poisDC"])
    print("Prayer Offence Bonus", total["prayOB"], "Prayer Defence Bonus", total["prayDB"])


goblinGen()

输出示例:

{'boots': 'nothing',
 'chest': 'nothing',
 'gloves': 'nothing',
 'head': 'nothing',
 'legs': 'bronzeChainlegs',
 'mainH': 'bronzeBAxe',
 'neck': 'nothing',
 'offH': 'woodenShield',
 'ring1': 'nothing',
 'ring2': 'nothing'}
Physical Attack: 23 Physical Defence: 5
Ranged Attack: 0 Ranged Defence: 6
Magic Attack: 0 Magic Defence: 2
Fire Attack Damage 0 Fire Defence Protection 0
Poison Attack Chance 0 Poison Defence Chance 0
Prayer Offence Bonus 0 Prayer Defence Bonus 0

相关问题 更多 >