CS 1073 Introductory Programming
for Scientific Applications
Random Walk
in the Plane
|
Java Program to carry out
a random walk in the plane:
The program below shows one implementation.
| Random Walk in the Plane |
public class RanWalk {
public final int MAXP = 40;
private char[][] plane = new char[MAXP][MAXP];
private String alf = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
"abcdefghijklmnopqrstuvwxyz";
private int x = MAXP/2, y = MAXP/2; /* current location */
private int steps = 0; /* number of steps taken */
public void blankPlane() {
for(int i = 0; i < MAXP; i++)
for(int j = 0; j < MAXP; j++)
plane[i][j] = ' ';
}
public void oneStep() {
if (Math.random() < 0.5) { /* change x */
if (Math.random() < 0.5) x = x + 1;
else x = x - 1;
}
else { /* change y */
if (Math.random() < 0.5) y = y + 1;
else y = y - 1;
}
}
public void printPlane() {
System.out.println("Number of steps to the boundary: " +
steps + "\n");
System.out.print(" ");
for(int j = 0; j < MAXP; j++)
System.out.print(j/10);
System.out.println("|");
System.out.print(" ");
for(int j = 0; j < MAXP; j++)
System.out.print(j%10);
System.out.println("|");
System.out.print(" ");
for(int j = 0; j < MAXP; j++)
System.out.print("-");
System.out.println("|");
for(int i = 0; i < MAXP; i++) {
if (i < 10) System.out.print(" " + i + "|");
else System.out.print(i + "|");
for(int j = 0; j < MAXP; j++)
System.out.print(plane[i][j]);
System.out.println("|");
}
System.out.print(" ");
for(int j = 0; j < MAXP; j++)
System.out.print("-");
System.out.println("|");
}
public void createRanWalk() {
while( x >= 0 && x < MAXP && y >= 0 && y < MAXP) {
steps++;
plane[x][y] = alf.charAt((steps/20)%52);
oneStep();
}
}
public static void main(String[] args) {
RanWalk ranW = new RanWalk();
ranW.blankPlane();
ranW.createRanWalk();
ranW.printPlane();
}
}
|
Features of this program:
- This program presents a random walk in the plane.
The program lays out a 40-by-40 square of characters in a
2-dimensional array. It start a path at the center.
Then randomly it moves on square up, down, left, or right,
each with equal probability.
- The program moves from square to square at random until it
first reaches the boundary. Then it stops and gives the number
of steps it took to get to the boundary.
- Each time the random walk reaches a square it puts a non-blank
character into that square. Instead of just putting a '*'
on the path, the program puts 10 'A's, then 10 B's, ...,
then 10 'Z's, then 10 'a's, ..., then 10 'z's, and then it starts
over with 'A' again. Thus after 1040 steps, it goes through the
upper- and lower-case letters, and then start over.
Here are results of runs.
Number of steps to the boundary: 1022
0000000000111111111122222222223333333333|
0123456789012345678901234567890123456789|
----------------------------------------|
0| |
1| |
2| |
3| |
4| |
5| |
6| |
7| |
8| |
9| |
10| |
11| |
12| |
13| |
14| |
15| VV VU |
16| VVVVVVUU |
17| WWVVUUSSS |
18|WWWW T T S ww|
19|WWXX TTT OSS A q xw|
20| W XXXX a OOSS AAA ppq yxx|
21|WW Zaa OOSS BBBAAA pppqv yyy|
22| aaaaYXPSSSHccBAAA oooo pvvvvvq yyy|
23| abbZYXPRRRQccccc iooppppuvuuvv yy |
24| abbbbbbbbbbbCCCc jkjooopsvvussvyzzz|
25| YYbQRRJJhhhhhhjkkkjjn vvusssyyw |
26| YYJNNNhhhH c kkii nntuu ww |
27| Y JNLMhhCHHcc kk nnnnuu |
28| hhhhLGGGc kk mm |
29| gMdeeLcccH ll mmm |
30| gggggeddG llmmmmmm |
31| ggggeGG llmm |
32| fggedF lll |
33| fggF FF l |
34| ffFFFFF |
35| fff FF |
36| ff FF |
37| ff |
38| |
39| |
----------------------------------------|
Number of steps to the boundary: 153
0000000000111111111122222222223333333333|
0123456789012345678901234567890123456789|
----------------------------------------|
0| HH |
1| H |
2| H |
3| HHH |
4| HH |
5| HH |
6| HH G |
7| GGGG |
8| FGG |
9| FFGG |
10| FF F DD |
11| FFFFE ED |
12| EEE EE |
13| E E |
14| EEEEE |
15| CDD |
16| B CDDD |
17| AACCCCCCCCCCCD |
18| AACCCB CC |
19| AA B |
20| A |
21| |
22| |
23| |
24| |
25| |
26| |
27| |
28| |
29| |
30| |
31| |
32| |
33| |
34| |
35| |
36| |
37| |
38| |
39| |
----------------------------------------|
Number of steps to the boundary: 592
0000000000111111111122222222223333333333|
0123456789012345678901234567890123456789|
----------------------------------------|
0| |
1| |
2| |
3| |
4| |
5| |
6| |
7| IIIII |
8| IHH |
9| I H HH |
10| I HHHHGG |
11| I HGGG |
12| I HHHLLF |
13| I KLLKL |
14| J KKKLLLLLL |
15| JJKKK EE LL |
16| S JKJJNNLLL |
17| SS NNNNNN |
18| SSP OOOOOJDNNN |
19| USTP O JDDMZ A |
20| UUTUSRR CBDBDZZAAA |
21| UUUTRROCCWZZZZYYY |
22| RbUQQO WZCZXZX Y |
23| dd ccbbbQVWWZZZZ XYYY |
24| ddddc Ubbaa ZZWW XXY |
25|dd c bbaaaaa WWWYY |
26| d cc bb W |
27| c cb |
28| ccc |
29| |
30| |
31| |
32| |
33| |
34| |
35| |
36| |
37| |
38| |
39| |
----------------------------------------|
Here's a run with a 60-by-60 square, instead of 40-by-40.
Number of steps to the boundary: 3024
000000000011111111112222222222333333333344444444445555555555|
012345678901234567890123456789012345678901234567890123456789|
------------------------------------------------------------|
0| v |
1| GGvv H |
2| JKJvvI HHH |
3| tJKuuIIHHHH |
4| ttJuuuHHHH |
5| uuuuuHHGG |
6| C uutKD |
7| CC suutDD |
8| FGGF EE DD sssst OD |
9| GGGGGFEEJEBDDDB rrrrss OOPPL |
10| GGFFG EJJEDB Drrr rssLOMPPPM |
11| FGGEEJJ DD Drr TTKK OOPPPD |
12| HHIIIJ DDDrr UTTT zNPOOD |
13| HITILMMJKB r UTTTT PPPz |
14| ITTLMPMNN r UUTT QQCyzyw |
15| TTTTLPPPPPAqrUUU CQQRyyyy |
16| RTTUUPPNPPzqVVVV QQRQyyyxx |
17| RRSUUVPP PPOqqqXVS uRBBww |
18| SjjvSSUVOOONNzOWqqqV uRRRww |
19| XXjjjSSwVVOOOOOOOWXXqq aaBRRRa |
20| XXXXjjjvSSVVx pLy qqqqq aaa BBRRb |
21| XXXWXh kjvrrVVyypppppRSXXYZZZARSSSeee q pp |
22| Y W hlmmvvuV xJpppyRRRXXYYYZAARSftbedsqpr |
23| YYY W ilmmmmVVpxxppppKQRPXXXY AASStttsssrprr |
24| YY WWhWlmlkuupxxoJppyQRP AAStt fsssssrr |
25| ZZ WhhhmmlloonxppNNoRRRO SSt cqssn rhh |
26| ZZZ mmnhkttomppppooRROO Stmmmooooohh |
27| YZZZaa m nngfnooooNooMI X SSSmgoonhhh ii |
28| YZZ aaa mnnnnnnfmmooNoII UW X S mlnon hiii |
29| aa nnfggggmmoooI UVVWW X TSS lnnkk jji |
30| a b eeCdFgGGmmHII VVVVWWWWXXTT nnlkkkkjj |
31| abbbeeEcFGG mmmIH VVVVWW WXXT n kkjkj |
32| aabcccccF lmlI VVV WZZZZX kk j |
33| bcbbcc FF llllG VV Z ZZ |
34| bcccc F lll Z YZZ |
35| bccc ggfffkkkk aaYZZ |
36| fgghhhkkk bbbaY |
37| hh ggkh bbb |
38| hiiigkk e ddbbb |
39| ijjjfkfff cddcc |
40| jjjjjk eeee d cc |
41| iijjk eeeddd ccc |
42| i j ddd c |
43| ii ddd |
44| i |
45| |
46| |
47| |
48| |
49| |
50| |
51| |
52| |
53| |
54| |
55| |
56| |
57| |
58| |
59| |
------------------------------------------------------------|
| Random Walk in the Plane |
public class RanWalkStat {
public final int MAXP = 60;
private int x, y; /* current location */
private int steps; /* number of steps taken */
public void init() {
steps = 0;
x = MAXP/2;
y = MAXP/2;
}
public void oneStep() {
if (Math.random() < 0.5) { /* change x */
if (Math.random() < 0.5) x = x + 1;
else x = x - 1;
}
else { /* change y */
if (Math.random() < 0.5) y = y + 1;
else y = y - 1;
}
}
public int runRanWalk() {
init();
while( x >= 0 && x < MAXP && y >= 0 && y < MAXP) {
steps++;
oneStep();
}
return steps;
}
public static void main(String[] args) {
RanWalkStat ranW = new RanWalkStat();
int[] walkStats = new int[80];
for (int i = 0; i < 100000; i++) {
int steps = ranW.runRanWalk();
if (steps/100 >= walkStats.length) {
walkStats[walkStats.length -1]++;
System.out.println(steps);
}
else walkStats[steps/100]++;
// System.out.println(steps);
}
for (int i = 0; i < walkStats.length; i++)
System.out.println(i*100 + "-" + ((i+1)*100 - 1) +
": \t" + walkStats[i]);
}
}
|
(Output slightly edited)
Range Number of Walks
0- 99: 4
100- 199: 872
200- 299: 4139
300- 399: 6989
400- 499: 8335
500- 599: 8323
600- 699: 8032
700- 799: 7573
800- 899: 6673
900- 999: 5858
1000-1099: 5223
1100-1199: 4574
1200-1299: 4151
1300-1399: 3628
1400-1499: 3140
1500-1599: 2789
1600-1699: 2472
1700-1799: 2086
1800-1899: 1903
1900-1999: 1643
2000-2099: 1519
2100-2199: 1193
2200-2299: 1149
2300-2399: 963
2400-2499: 829
2500-2599: 758
2600-2699: 633
2700-2799: 536
2800-2899: 506
2900-2999: 433
3000-3099: 391
3100-3199: 317
3200-3299: 296
3300-3399: 238
3400-3499: 247
3500-3599: 201
3600-3699: 171
3700-3799: 156
3800-3899: 159
3900-3999: 109
4000-4099: 105
4100-4199: 86
4200-4299: 70
4300-4399: 62
4400-4499: 53
4500-4599: 57
4600-4699: 35
4700-4799: 49
4800-4899: 29
4900-4999: 26
5000-5099: 31
5100-5199: 21
5200-5299: 26
5300-5399: 18
5400-5499: 17
5500-5599: 9
5600-5699: 17
5700-5799: 7
5800-5899: 8
5900-5999: 11
6000-6099: 5
6100-6199: 6
6200-6299: 6
6300-6399: 6
6400-6499: 1
6500-6599: 2
6600-6699: 5
6700-6799: 2
6800-6899: 2
6900-6999: 3
7000-7099: 3
7100-7199: 0
7200-7299: 1
7300-7399: 1
7400-7499: 3
7500-7599: 0
7600-7699: 0
7700-7799: 1
7800-7899: 0
7900-7999: 5
(plus walks of lengths: 8073, 9043, and 9708)