-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNrandomwalks.java
More file actions
31 lines (29 loc) · 1.02 KB
/
Nrandomwalks.java
File metadata and controls
31 lines (29 loc) · 1.02 KB
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
import java.io.*;
public class Nrandomwalks{
public static void main(String[] args) throws FileNotFoundException {
PrintStream output = new PrintStream(new File ("NrandomWalks.txt"));
int trials = 500000;
int N = 20;
double[] Xaccum = new double[N+1];
double[] Xsquaredaccum = new double[N+1];
double p = .7;
for (int i = 0; i< trials; i++){
int position = 0;
for (int x = 0; x < N; x++){
double rando = Math.random();
if (rando <= p){
position--;
} else{
position++;
}
Xaccum[x+1] += position;
Xsquaredaccum[x+1] += Math.pow(position, 2);
}
}
for(int g=0; g<=N; g++){
double dummy=1.0*Xaccum[g]/trials;
double dummy2=1.0*Xsquaredaccum[g]/trials-dummy*dummy;
output.printf("%-6d %-10.5f %-10.5f \n",g,dummy, dummy2);
}
}
}