Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#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

image text in transcribed

#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 Exercise 1. (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) and with equal probabilities goes up, right, down and left. For example, when the walker is at (r,y), with equal probability 1/4, his next location is at (x, y 1), (1,), (,y+1), or (-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 he touches one of the edges of the square, given he starts his walk from (0, 0) One extreme example is n = 1, when the walker starts from (0,0), after one step, he will touch one of the edges of the square. There is only 1 point inside the square and the walk visits this point before he touches one of the edges. Therefore, this faction is 1 for 1 In the stater code 2d-random.c, implement the following function double two d random(int n) This function should return the fraction mentioned above. n is the integer mentioned above to define the square boundary In the function, we first allocate memory for all the integer (x y) coordinates inside the square boundary This is because we need to keep track which coordinates have been visited. Allocate just one memory block and figure out how we map between (x, y) to the index of the array(we can treat this allocated memory block as an integer array). We can 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; and treat r 0,1,2, 3 as going 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 (, ) coordinates inside (not including) the square. Reminder: Do not forget to free the allocated memory block before the function ends

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

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2017 Skopje Macedonia September 18 22 2017 Proceedings Part 3 Lnai 10536

Authors: Yasemin Altun ,Kamalika Das ,Taneli Mielikainen ,Donato Malerba ,Jerzy Stefanowski ,Jesse Read ,Marinka Zitnik ,Michelangelo Ceci ,Saso Dzeroski

1st Edition

3319712721, 978-3319712727

More Books

Students also viewed these Databases questions

Question

Does it exceed two pages in length?

Answered: 1 week ago

Question

Does it avoid typos and grammatical errors?

Answered: 1 week ago