Repository navigation
Expand file tree
/
Copy pathSelectiveAmnesia.java
More file actions
89 lines (75 loc) · 1.67 KB
/
Copy pathSelectiveAmnesia.java
File metadata and controls
89 lines (75 loc) · 1.67 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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package Amnesia;
import java.util.Random;
public class SelectiveAmnesia {
private final static int MAX = 10000000;
public static void main(String[] args) {
SelectiveAmnesia sa = new SelectiveAmnesia();
int[] memoryL = new int[5];
int[] memoryR = new int[5];
int[] added = new int[11];
int[] called = new int[11];
int L = 0,R = 0;
double sum = 0;
boolean found = false;
for(int i=0;i<MAX;i++){
//System.out.println("Computation: " + i);
for(int j=0;j<50;j++){
int call = sa.numberCall();
found = false;
for(int k=0;k<5;k++){
if(memoryL[k] == call){
found = true;
break;
}
}
if(!found){
sa.addL(memoryL,added,call);
added[call] = j;
}
else{
L++;
}
found = false;
for(int k=0;k<5;k++){
if(memoryR[k] == call){
found = true;
called[call] = j;
break;
}
}
if(!found){
sa.addR(memoryR,called,call);
called[call] = j;
}
else{
R++;
}
//System.out.println("Iteration " + j + ": call = " + call +", L = " + L + ", R = " + R);
}
sum += Math.abs(L - R);
//System.out.println("Sum = " + sum);
}
System.out.println("Expected value: " + sum/MAX);
}
private void addL(int[] memory,int[] added,int call){
int i,minI = 0;
for(i=0;i<5;i++){
if(added[memory[i]] < added[memory[minI]]){
minI = i;
}
}
memory[minI] = call;
}
private void addR(int[] memory,int[] called,int call){
int i,minI = 0;
for(i=0;i<5;i++){
if(called[memory[i]] < called[memory[minI]]){
minI = i;
}
}
memory[minI] = call;
}
private int numberCall(){
return (new Random().nextInt(10) +1);
}
}