簡體   English   中英

Counter()返回看似隨機的大整數

[英]Counter() returning seemingly random large integers

請原諒大量的代碼塊,但我根本無法弄清楚出了什么問題。 據我了解,這應該工作。 焊接清單是隨機分配的設備,然后將所有匹配值相加。 但是由於某種原因,它會返回大的幾乎隨機數。 我真的需要幫助

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

所以,根本的問題是你的突變nothing計數器,所以當你生成若干li'l gobbos,結果累積。 通過實例化新的總計計數器來解決此問題。

我也修復了其他幾件事:

  • 設備信息不再是所有Counter ,因為這是不必要的。
  • 您無需在getRandomWeightedElement使用**字典解包語法。
  • 地精選擇的設備現在存儲在字典中,因此您一眼就能看到哪個插槽。 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM