A drunkard in a grid of streets randomly picks one of four directsions and stumb
ID: 3545840 • Letter: A
Question
A drunkard in a grid of streets randomly picks one of four directsions and stumbles to the next intersection, then again randomly picks one of four directions, and so on. You might think that on average the drunkard doesn't move very far because the choices cancel each other out, but that is not the case. Represent locations as integer pairs (x,y). Implement the drunkard's walk over 100 intersections, starting at (0,0) and print the ending location
again this is in java.
also i googled this and was able to find some solutions that didn't work at all so please don't just copy + paste things if you haven't tested them or written them yourself. thank you :)
Explanation / Answer
import java.util.*;
public class intersection
{
public static void main(String args[])
{
int x = 0;
int y = 0;
System.out.println("Initial point is ("+x+","+y+")");
Random rand_obj = new Random();
int direction,distance;
for(int i=0; i<100; i++)
{
direction = rand_obj.nextInt(4);
distance = rand_obj.nextInt(100);
if(direction == 0)
x = x + distance;
else if(direction == 1)
y = y + distance;
else if(direction == 2)
x = x - distance;
else
y = y - distance;
}
System.out.println("Final point is ("+x+","+y+")");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.