I need the function to be able to walk in all four directions with equal probabbility and return the distance away from the starting point. The function below only takes steps forwards and backwards?

def main():
    numWalks, numSteps = getInputs()
    averageSteps = takeWalks(numWalks, numSteps)
    printExpectedDistance(averageSteps)

def getInputs():
    numWalks = input("How many random walks to take? ")
    numSteps = input("How many steps for each walk? ")
    return numWalks, numSteps

def takeWalks(numWalks, numSteps):
    totalSteps = 0
    for walk in range(numWalks):
        stepsAway = takeAWalk(numSteps)
        totalSteps = totalSteps + stepsAway
    return float(totalSteps) / numWalks

def printExpectedDistance(averageSteps):
    print "The expected number of steps away from the "
    print "start point is", averageSteps

def takeAWalk(numSteps):
    from random import random
    stepsForwardOfStart = 0
    for step in range(numSteps):
        if random() < 0.5:
            stepsForwardOfStart = stepsForwardOfStart - 1
    else:
        stepsForwardOfStart = stepsForwardOfStart + 1
        return abs(stepsForwardOfStart)

main()

Dani AI

Generated

There are two separate issues in the original post: a Python indentation/logic bug that makes the walk biased, and the missing handling for four directions. As hinted, using a 2D coordinate (x,y) is the cleanest solution. The bug in the posted takeAWalk is that the else lines up with the for, so Python treats it as a for-else rather than an if-else. That causes only decrements during the loop and a single +1 after the loop finishes. Move the else to match the if, and make sure return happens after the loop, not inside it.

A minimal 4-direction walker (Euclidean distance) looks like this:

import random

def walk_distance(steps):
    x = 0
    y = 0
    moves = [(1,0), (-1,0), (0,1), (0,-1)]
    for _ in range(steps):
        dx, dy = random.choice(moves)
        x += dx
        y += dy
    return (x*x + y*y) ** 0.5

If you prefer Manhattan distance (grid steps away), return abs(x) + abs(y) instead. When running many trials, average the returned distance across walks (sum / num_walks). Also be careful with input conversion: in Python 3 use int(input(...)); in Python 2 prefer int(raw_input(...)) to avoid input() evaluating arbitrary code.

For a sanity check: for a 2D simple random walk the mean squared distance after n unit steps is n, so RMS distance scales like sqrt(n). The expected distance (large n) is approximately (sqrt(pi)/2) * sqrt(n) (~0.886 * sqrt(n)). Use that to verify your simulation converges as numWalks grows.

one way to do this would be to use a coordinate system to store the current position (ie x,y) and then calculate the distance from that position to the starting position using trigonometry. your random direction routine would need to then handle 4 states: up (y + 1), down (y - 1), left (x - 1) and right (x + 1)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.