random walk

    xiaoxiao2022-07-07  165

    # Numpy is imported, seed is set import numpy as np np.random.seed(123) # Initialize random_walk random_walk = [0] # Complete the ___ for x in range(100) : # Set step: last element in random_walk step = random_walk[-1] # Roll the dice dice = np.random.randint(1,7) # Determine next step if dice <= 2: # step = step - 1 step = max(0, step - 1) # use max to make sure step can't go below 0 elif dice <= 5: step = step + 1 else: step = step + np.random.randint(1,7) # append next_step to random_walk random_walk.append(step) # Print random_walk print(random_walk) # plot random_walk import matplotlib.pyplot as plt plt.plot(random_walk) plt.show()

    distribution
    # Numpy is imported, seed is set import numpy as np np.random.seed(123) # Initialize all_walks (don't change this line) all_walks = [] # Simulate random walk 10 times for i in range(10): # Code from before 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) # Append random_walk to all_walks all_walks.append(random_walk) # Print all_walks print(all_walks) # a list of lists: every sub-list represents a single random walk. # Convert all_walks to Numpy array: np_aw np_aw = np.array(all_walks) # Plot np_aw and show import matplotlib.pyplot as plt plt.plot(np_aw) plt.show() # Clear the figure plt.clf() # Transpose np_aw: np_aw_t np_aw_t = np.transpose(np_aw) # Plot np_aw_t and show plt.plot(np_aw_t) plt.show()

    import numpy as np import matplotlib.pyplot as plt np.random.seed(123) # Simulate random walk 500 times all_walks = [] for i in range(500) : 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) if np.random.rand() <= 0.001 : step = 0 random_walk.append(step) all_walks.append(random_walk) # Create and plot np_aw_t np_aw_t = np.transpose(np.array(all_walks)) # Select last row from np_aw_t: ends ends = np_aw_t[-1,:] # Plot histogram of ends, display plot plt.hist(ends) plt.show()

    最新回复(0)