-
Notifications
You must be signed in to change notification settings - Fork 0
/
RandomWalker.java
75 lines (66 loc) · 2 KB
/
RandomWalker.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import java.util.Scanner;
public class RandomWalker {
// Initial Position of the rabbit
private int axis_x = 0;
private int axis_y = 0;
private int n = 0;
private int trails = 0;
double distance = 0.0;
RandomWalker(int n,int trails){
this.n = n;
this.trails = trails;
}
public void getParameter(){
Scanner input = new Scanner(System.in);
System.out.println("Please input n and trials");
this.n = input.nextInt();
this.trails = input.nextInt();
}
public void move()
{
int num = -1;
for(int i =1;i<n+1;i++) {
num = 0;
num = 1+(int) (Math.random() * 4);
switch (num) {
case 1:
axis_x++;
break;
case 2:
axis_x--;
break;
case 3:
axis_y++;
break;
case 4:
axis_y--;
break;
}
}
}
public void Distance(){
double[] Trails = new double[trails];
for(int i =0;i<trails;i++){
move();
distance = Math.sqrt(axis_x * axis_x + axis_y*axis_y);
Trails[i] = distance;
}
distance = 0;
for(int i =0;i<Trails.length;i++){
distance += Trails[i];
}
System.out.printf("Now the Distance is %.3f\n",distance);
}
public static void main(String[] args) {
RandomWalker rb1 = new RandomWalker(100,1);
rb1.Distance();
RandomWalker rb2 = new RandomWalker(1000,1);
rb2.Distance();
RandomWalker rb3 = new RandomWalker(1000,2);
rb3.Distance();
RandomWalker rb4 = new RandomWalker(1000,3);
rb4.Distance();
RandomWalker rb5 = new RandomWalker(10000,3);
rb5.Distance();
}
}