Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Program in C : 2D Random Walk Write a program to simulate two random walkers in 2D finite space o Define a rectangular space of

Program in C :

2D Random Walk Write a program to simulate two random walkers in 2D finite space o Define a rectangular space of height M and width N. Place each walker in this space at a randomly chosen location (x,y, x<=M and y<=N). Step size is 1 for both walkers. The direction of each step is random and independent. The walkers can only visit a given space only once. o Run the simulation until one of the following occurs: 1) Both walkers get stuck and can't move to an open space, or 2) when either walker crosses path with the other walker. In other words, the next step of a walker happens to be occupied by the other walker. Count the number of steps and the reason for stopping the simulation. o Run the simulation many times and plot the number of steps when walkers stop moving.

Starter code:

#include #include

double two_d_random(int n) {

//Fill in code below //You are fee to write some other functions this function //Allocate memory for all the integer (x, y) coordinates inside the boundary //Allocate just one memory block //Figure out how to map between (x, y) and the index of the array //We can treat this allocated memory block as an integer array //Write a function for this mapping if needed.

//When deciding which way to go for the next step, generate a random number as follows. //r = rand() % 4; //Treat r = 0, 1, 2, 3 as moving up, right, down and left respectively.

//The random walk should stop once the x coordinate or y coordinate reaches $-n$ or $n$. //The function should return the fraction of the visited $(x, y)$ coordinates inside (not including) the square.

//Do not forget to free the allocated memory block before the function ends.

}

//Do not change the code below int main() { int trials = 1000;

srand(12345); for(int n=1; n<=256; n*=2) { double sum = 0.; for(int i=0; i < trials; i++) { double p = two_d_random(n); sum += p; } printf("%d %.3lf ", n, sum/trials); } return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions