首页 > 解决方案 > Finding the error in my Python function, which is supposed to return a list [average_steps, list_of_steps], and plot a histogram of steps?

问题描述

The direction/guidelines for the function is as following: Consider a random walk in two dimensions. A walker starts at the origin (0, 0) and takes steps each of unit length and each in a completely random direction until reaching a distance D or further away from the origin. Write a function monteCarloRandomWalkFirstPassage that performs nTrials of such an experiment, return a list [average_steps, list_of_steps], where the average_steps means the average number of steps required to first reach a distance D or more from the origin, and the list_of_steps is a list of nTrials numbers, recording the number of steps for each trial. Also, plots a histogram of the distribution of number of steps required.

You will want to write a helper function randomWalkFirstPassageTrial(D) that returns the number of steps required to reach a distance D or more away for one trial of such an experiment. One random walk trial consists of a walker starting at the origin (0,0) taking unit steps in the theta = random.uniform(0, 2*math.pi) direction. The change in x and y (the size of the step) are math.cos(theta) and math.sin(theta), respectively. At any point in the walk, the distance is given by math.sqrt(x2 + y2).

You will want to use the plotHistogram(myList, binMin, binMax, nBins, title) function from hist.py. This function will help you plot a histogram. Again, remember to download the hist.py module and the cTurtle.py module in your working directory first. In this function, myList means a list of values that you would like to plot into a histogram; binMin means the minimal (left) boundary of the histogram. For those elements in myList that are less than binMin, they will be put into the first bin. If binMin is a boolean value, then the minimal boundary will be the minimal element in myList. binMin is defaulted as False. binMax means the maximal (right) boundary of the histogram. For those elements in myList that are greater than binMax, they will be put into the last bin. If binMax is a boolean value, then the maximal boundary will be the maximal element in myList. binMax is defaulted as False. nBins means the number of bins in the histogram. nBins is defaulted as 20. title is the title of the histogram. title is defaulted as ''. First, define a list of random numbers randomList, and then test the function plotHistogram.

This is the code I have created so far:

import hist
import math

def randomWalkFirstPassageTrial(D):
steps = 0
x = 0
y = 0
Dist = 0
while Dist < D:
    theta = random.uniform(0,2*math.pi)
    x += math.cos(theta)
    y += math.sin(theta)
    steps += 1
    Dist = math.sqrt(x**2 + y**2)
return steps

def monteCarloRandomWalkFirstPassage(D, nTrials):
    steps = 0
    L = []
    for i in range(nTrials):
        x = randomWalkFirstPassageTrial(D)
        steps += x
        L.append(x)
    average_steps = steps/nTrials
    hist.plotHistogram(L)
    return L

My autograder for this assignment returns an error that: "Test Failed: 94 != 109.829 within 10 delta" with varying numbers among different tests.

Where is the error in my code? I can't seem to find where I went wrong. Any and all help is appreciated!

标签: pythonpython-3.xstatisticshistogram

解决方案


推荐阅读