In this assigment, we will take a look at and analyze two and three-dimensional random walks. Random walks describe physical processes such as the movement of particles through space.
Unlike nature, computers can't truly generate totally 'random' numbers. We can use functions in C++ to try to generate a few random numbers.
A few functions are the rand() and drand48() functions in C++. Using these functions require a 'seed' value, which the function takes in
to generate random numbers each time. One way of doing this is using the srand48(time(NULL))[1] function, which uses the current time as
a seed value to start randomizing numbers so that each time a program is run, we get new random results.
To plot a random walk, we write a program to generate random numbers and plot each point as a vector pointing to each successive vector
as a path. This will be done in 2D on the xy-plane and in 3D on the xyz-plane. Then, we analyze the results of the graph
by curve fitting.
C++ files: 2D Random Walk, 3D Random Walk
The drand48() function generates pseudo-random numbers with 48-bit integer arithmetic. This was used to generate random numbers over
the interval [-0.5,0.5]. We want an equal probability of the next step to move left, right, up, or down, so we multiply the interval by
for random walks in two dimensions and
for three dimensions[2]. Since each random number
point represents a vector position, we can obtain the distance with
. The 3D random walk program is identical to
the 2D one, with the added variable of z.
Gnuplot was used to plot the following random walk graphs from the output files generated by the program. The graphs below show results for 100 and 1000 steps in both 2D and 3D.
![]() Figure 1: 2D random walk at 100 steps |
![]() Figure 2: 2D random walk at 1000 steps |
![]() Figure 3: 3D random walk at 100 steps |
![]() Figure 4: 3D random walk at 1000 steps |
![]() Figure 6: Log-log fit of distance traveled vs. number of steps in 2D |
![]() Figure 8: Log-log fit of distance traveled vs. number of steps in 3D |
This assignment allowed us to simulate random walks of nature using deterministic computational values and this does show that it may be a valid representation of natural random walks, according to what the graphs show in the analysis. The seed value we used comes from a constant based on the current time and that looks like it has been reliable in generating random values each time the program is run. However, units of time are 'fixed', as in there will always be 60 seconds in a minute and 60 minutes in an hour, and etc, so there may be a case where we'll replicate similar patterns because of the repetition of the units of time.
[1] David Yevick "A First Course in Computational Physics and Object-Oriented Programming with C++", Cambridge University Press 2005,
page 60.
[2] PHYS 305 Assignment #5. Varner, Gary.