Day 1 - Numpy Revision
Numpy Revision
We made a python program for the following random walk question:
Say you are standing at the bottom of a staircase with a dice. With each throw of the dice, you either move down one step (if you get a 1 or 2 on the dice) or move up one step (if you get a 3, 4 or 5 on the dice). If you throw a 6 on the dice, you throw the dice again and move up the staircase by the number you get on that second throw. Note if you are on the base of the staircase you cannot move down! What is the probability that you will reach more than 60 steps after 250 throws of the dice?
This question required the knowledge of np.random.seed. It seeds the generator. It makes the random numbers predictable. When the value is reset, the same numbers will appear every time. If a seed is not assigned, NumPy automatically selects a random seed value based on the system's random number generator device or on the clock
The code was as follows:
import numpy as np
np.random.seed(123)
allwalks = []
for i in range(250):
randwalk = [0]
for x in range(100):
step = randwalk[-1]
dice = random.randint(1,7)
if dice <= 2 :
step = max(0, step - 1)
elif dice<=5:
step += 1
else:
step = step + random.randint(1,7)
print(step)
Comments
Post a Comment