vendredi 13 novembre 2020

Random Walk Steps Based on n Dice Rolls

I'm writing a function that takes 3 inputs P, Q, N and returns the location of a walker that steps based on the rolls of N dice according to following:

  • If the outcome of the die is 1, the walker takes P steps toward east.

  • If the outcome of the die is 2, the walker takes Q steps toward south.

  • If the outcome of the die is 3, the walker takes Q+P steps toward north.

  • If the outcome of the die is 4, the walker takes Q steps toward west.

  • If the outcome of the die is 5, the walker takes P steps toward north and Q steps toward east.

  • If the outcome of the die is 6, the walker takes Q steps toward south and P steps toward west.

And the function returns the final location of the walker with a list of two strings: for example ['3E', '4S'] shows at the end the walker has taken 3 steps toward the east and 4 steps toward south.

import numpy.random

def walk_dice(P, Q, N):
    ans = list(numpy.random.randint(1,7,N))
    if 1 in ans:
        pos[0]+P
    elif 2 in ans:
        pos[1]-Q
    elif 3 in ans:
        pos[1]+P+Q
    elif 4 in ans:
        pos[0]-Q
    elif 5 in ans:
        pos[0]+Q
        pos[1]+P
    elif 6 in ans:
        pos[0]-P
        pos[1]-Q
    return pos

numpy.random.seed(seed=10)
print(walk_dice(3, 4, 2))
#[1, 2]

I get the same output everytime. Where am I going wrong with this code?




Aucun commentaire:

Enregistrer un commentaire