Question: Exercise 2 . ( 5 0 points ) Bounded 2 D Random Walk In this problem, we will write a program to simulate a 2

Exercise 2.(50 points) Bounded 2D Random Walk
In this problem, we will write a program to simulate a 2D random walk. Imagine a random walker starting at the origin (0,0) that with equal probabilities goes up, right, down and left. For example, when the walker is at (x,y), with equal probability 14, their next location is at (x,y-1),(x+1,y),(x,y+1), or (x-1,y).
Given a positive integer n, a square is defined by the following four points: (-n,-n),(-n,n),(n,n), and (n,-n). We are interested in knowing, on average, what fraction of points within this square the walker visits before they touch one of the edges of the square, given they start their walk from (0,0).
two_d_random(n)
1,000 times and calculate and print out the mean of the covered fractions for the given n.
//Do not change the code below
int main(int argc, char* argv[])
{
int trials =1000;
int i,n, seed;
if ( argc==2) seed = atoi(argv[1]);
else seed =12345;
srand (seed);
for )=1;n64;n**=(2
{
double sum =0.;
for trials; i++
{
double p= two_d_random(n);
}
sum +=p;
}
printf("%d %.3lf
", n, sum/trials);
return 0 ;
}
Below is the desired output. We can use the output to check our code. You can optionally set the seed for the random numbers generated as a command line argument with ./2d-walk [seed]
\table[[$,.?2d-walk],[1,1.000],[2,0.367],[4,0.221],[8,0.154],[16,0.122],[32,0.101],[64,0.085]]
 Exercise 2.(50 points) Bounded 2D Random Walk In this problem, we

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!