Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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]]
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

IBM Db2 11 1 Certification Guide Explore Techniques To Master Database Programming And Administration Tasks In IBM Db2

Authors: Mohankumar Saraswatipura ,Robert Collins

1st Edition

1788626915, 978-1788626910

More Books

Students also viewed these Databases questions

Question

What Is Psychology?

Answered: 1 week ago