Random Walk Generation with NumPy

  • Share this:

Code introduction


This function generates a random walk using NumPy. A random walk is a statistical model that describes the process of a random variable changing over time or space.


Technology Stack : NumPy

Code Type : Function

Code Difficulty : Intermediate


                
                    
import numpy as np

def random_walk(num_steps, step_size):
    """
    Generates a random walk using NumPy.
    """
    # Initialize the starting point of the walk
    walk = np.zeros(num_steps)
    
    # Generate random steps
    for i in range(1, num_steps):
        step = np.random.normal(0, step_size)
        walk[i] = walk[i - 1] + step
    
    return walk

# Code Information                
              
Tags: