mercredi 5 juillet 2017

Python: More efficient way to create bias for random.choice()

I'm currently designing a simple python script that uses the pyautogui module in order to randomly play an emulator of Pokemon Red. The script essentially uses pyautogui to press a random key in a set of keys that correspond to the buttons in the game.

In order to speed up the game, I am attempting to place a certain bias on each of the possible input keys. Right now, I am doing that by creating an array of one thousand possible inputs with higher biased keys having more entries. That code looks like this:

upWeight = 175
downWeight = 175
leftWeight = 175
rightWeight = 175
xWeight = 150
zWeight = 100
enterWeight = 50

possibleInputs = [];

iCnt = 0
while iCnt < upWeight:
    possibleInputs.append('up')
    iCnt += 1
iCnt = 0
while iCnt < downWeight:
    possibleInputs.append('down')
    iCnt += 1
iCnt = 0
while iCnt < leftWeight:
    possibleInputs.append('left')
    iCnt += 1
iCnt = 0
while iCnt < rightWeight:
    possibleInputs.append('right')
    iCnt += 1
iCnt = 0
while iCnt < xWeight:
    possibleInputs.append('x')
    iCnt += 1
iCnt = 0
while iCnt < zWeight:
    possibleInputs.append('z')
    iCnt += 1
iCnt = 0
while iCnt < enterWeight:
    possibleInputs.append('enter')
    iCnt += 1

...

while jCnt < 10000:
        pyag.press(rand.choice(possibleInputs))
        jCnt += 1

It feels as though there should be a much more concise way to achieve a choice bias in python.

How might I go about writing this more efficiently?




Aucun commentaire:

Enregistrer un commentaire