a drunkard begins walking aimlessly, starting at a lampost. at each time step, t
ID: 3649400 • Letter: A
Question
a drunkard begins walking aimlessly, starting at a lampost. at each time step, the drunkard forgets where he is, and takes one step at random, either north, east, south, or west, with probability 25 percent.How far will the drunkard be from the lamp post after N steps?
Write a program that takes an integer command-line argument N and simulates the motion of a random walker for N steps. After each step, print the location of the random walker, treating the lamp post as the origin. Also print the square of the final distance from the origin.
Explanation / Answer
This week we look at two different cases involving random walks. In the first case we will consider an unconstrained 2-dimensional random walk which can move in any direction at each step, up to a maximum step length. This is just one of an infinite variety of random walk possibilities, many of which have interesting physics applications. Some of these possible applications are: Electron or ion transport in crystal lattices Brownian (thermal) motion of microscopic particles in suspension Spark discharge paths in gases Macromolecular growth and polymerization Genetics & biology (eg. DNA & RNA, and protein production processes) To get started on random walks, we need to first understand how computers produce random numbers and how we can use them. Pseudo-random numbers Computers cannot generate truly random numbers since they are themselves deterministic machines. However, nature does have many seemingly random processes that we would like to simulate in physics modeling. So we need a way to approximate random numbers with the computer. In fact we often do not want the numbers to be truly random, because then we could not go back and reproduce results we may have gotten with a purely random number generator, since we could not reproduce exactly the sequence that generated those results. Generating pseudo-random numbers is usually done using a sequential algorithm that involves calculating a new number based on an old one, using the fact that the bit sequences associated with the binary representation of a number in the computer are often nearly random, at least for some length of a sequence. As an introduction, consider the (partial, here) man pages for "random", the linux random number generator for C: RANDOM(3) Linux Programmer’s Manual RANDOM(3) NAME random, srandom, initstate, setstate - random number generator. SYNOPSIS #include long int random(void); void srandom(unsigned int seed); char *initstate(unsigned int seed, char *state, size_t n); char *setstate(char *state); DESCRIPTION The random() function uses a non-linear additive feedback random number generator employing a default table of size 31 long integers to return successive pseudo-random numbers in the range from 0 to RAND_MAX. The period of this random number generator is very large, approximately 16*((2**31)-1). The srandom() function sets its argument as the seed for a new sequence of pseudo-random integers to be returned by random(). These sequences are repeatable by calling srandom() with the same seed value. If no seed value is provided, the random() function is automatically seeded with a value of 1. RETURN VALUE The random() function returns a value between 0 and RAND_MAX. The srandom() function returns no value. The initstate() and setstate() functions return a pointer to the previous state array, or NULL on error. note there are several things to take account of: you need to add the statement #include to any program in which you wish to use random. Also, and very importantly, this program returns a long integer--not a floating point number--which is between 0 and RAND_MAX, the largest integer value for the computer: /usr/include/stdlib.h:#define RAND_MAX 2147483647 To get floating point random numbers, we can use "drand48" and its relatives: DRAND48(3) Linux Programmer’s Manual DRAND48(3) NAME drand48, erand48, lrand48, nrand48, mrand48, jrand48, srand48, seed48, lcong48 - generate uniformly distributed pseudo-random numbers SYNOPSIS #include double drand48(void); double erand48(unsigned short xsubi[3]); long int lrand48(void); long int nrand48(unsigned short xsubi[3]); long int mrand48(void); long int jrand48(unsigned short xsubi[3]); void srand48(long int seedval); DESCRIPTION These functions generate pseudo-random numbers using the linear congru- ential algorithm and 48-bit integer arithmetic. The drand48() and erand48() functions return non-negative double-preci- sion floating-point values uniformly distributed between [0.0, 1.0). The lrand48() and nrand48() functions return non-negative long integers uniformly distributed between 0 and 2^31. The mrand48() and jrand48() functions return signed long integers uni- formly distributed between -2^31 and 2^31. The srand48(), seed48() and lcong48() functions are initialization functions, one of which should be called before using drand48(), lrand48() or mrand48(). The functions erand48(), nrand48() and jrand48() do not require an initialization function to be called first.Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.