Skip to content

Instantly share code, notes, and snippets.

@sengupta
Created May 8, 2011 18:33
Show Gist options
  • Save sengupta/961578 to your computer and use it in GitHub Desktop.
Save sengupta/961578 to your computer and use it in GitHub Desktop.
One Dimensional Random Walk
// One Dimensional Random Walk
// http://en.wikipedia.org/wiki/Random_walk
PossibleSteps = [1, -1] // Assuming you can only go ahead by one step or backwards by one step
Start = 0 // Start at 0 on the number line
NumberOfSteps = 1000
StepsTaken = sample(NumberOfSteps, PossibleSteps); // Take a random step ahead or back
n = length(StepsTaken)
Path = StepsTaken*triu(ones(n, n)); // Find the cumulative position
plot2d(Path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment