The Monte Carlo method generates walks with different using the correct probabilities to ensure that the sampled walks are representative of the total population of possible walks. Clearly, the more walks we use, the close we will come to the exact result. In practice, we obtain the desired accuracy by comparing our results to the increasing number of trials until the results do not appear to vary within a tolerance.
The walker performs a steps to the right with probability , and a step to the left with probability . To simulate this, we pick a random number between 0 and 1. If , we move one step to the right, otherwise, we move one step to the left. We repeat this operation times to obtain the net displacement , and we accumulate the result to calculate the averages.
The pseudocode to generate a random configuration of steps would look like this:
In order to determine the probability we need to perform many trials and record the fraction of times that a walker is at position after steps. All the other quantities can be calculated at the end using the values we obtained for .