Plotting random dice walk

PHOTO EMBED

Fri Nov 26 2021 10:49:17 GMT+0000 (Coordinated Universal Time)

Saved by @Sourabh #list #forloop #for #loop #dicegame #matplotlib #[plot #graph

# Numpy is imported, seed is set

# Initialization
random_walk = [0]

for x in range(100) :
    step = random_walk[-1]
    dice = np.random.randint(1,7)

    if dice <= 2:
        step = max(0, step - 1)
    elif dice <= 5:
        step = step + 1
    else:
        step = step + np.random.randint(1,7)

    random_walk.append(step)

# Import matplotlib.pyplot as plt
import matplotlib.pyplot as plt

# Plot random_walk
plt.plot(random_walk)

# Show the plot
plt.show()
content_copyCOPY