Repository navigation
Expand file tree
/
Copy pathtesto.js
More file actions
154 lines (88 loc) · 3.73 KB
/
Copy pathtesto.js
File metadata and controls
154 lines (88 loc) · 3.73 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/// BROKEN OUT FUNCTIONS
// Function replaces character in progress string if a match is found
// between the random string and the target string.
function setCharAt(str,index,chr) {
if(index > str.length-1) return str;
return str.substr(0,index) + chr + str.substr(index+1);
}
// Function creates a target string for each trial.
function createTargetString(){
targetString="";
for ( var n=0; n < targetLength; n++ ) {
var rnum = Math.floor(Math.random() * possibleCharacters.length);
targetString += possibleCharacters.substring(rnum,rnum+1);
}
return targetString;
}
// Function creates a random string to compare to the target string
// for each generation.
function createRandomString(){
randomString = "";
for ( var i=0; i < targetString.length; i++ ) {
var rnum = Math.floor(Math.random() * possibleCharacters.length);
randomString += possibleCharacters.substring(rnum,rnum+1);
}
return randomString;
}
// Function creates the progress string of empty characters
function createProgressString(){
var progressString = "";
for (var c=0; c < targetString.length; c++) {
progressString = progressString + "*";
}
return progressString;
}
// Function loops through target string and compares each character
// to the random string. Replaces character on progress string if match.
function loopCheckReplace(){
for (var p = 0; p < targetString.length; p++) {
var first_char = randomString.slice(p, p+1);
var second_char = targetString.slice(p, p+1);
if (first_char === second_char) {
progressString = setCharAt(progressString, p, second_char);
}
}
}
// Function gets an average of the number of generations required for the trials
// to get from the progress string to the target string.
function getAverageGenerations(){
var add = 0;
for (var u = 0; u < generationsRequiredResults.length; u++){
add = add + generationsRequiredResults[u];
}
return add / u;
}
// VARIABLES FOR EXPERIMENT
// Variable sets the number of characters for the strings to be tested.
// Begins at 2 and ends at maximum number.
var maximumStringLength = 10000;
// Variable sets the number of trials to generate an average number of
// generations to get to the target string.
var numberOfTrials = 10000;
// Variable sets the characters that are used to generate the target
// string and also the random string for each generation.
var possibleCharacters = " abcdefghiklmnopqrstuvwxyz";
// SCRIPT
// Loop begins experiments at a string of two characters and continues up
// to the maximum string length specified for the overall experiment.
for (var targetLength = 2; targetLength < maximumStringLength; targetLength++){
targetString = createTargetString();
var generationsRequiredResults = [];
// This loop continues until the number of trials is complete.
for (var r = 0; r < numberOfTrials; r++){
var counter = 0;
var progressString = createProgressString();
// Trial Loop continues until progress string equals the target string.
do {
var counter = counter + 1;
var randomString = createRandomString();
loopCheckReplace();
} while (targetString != progressString);
generationsRequiredResults[r] = counter;
}
var averageGenerations = getAverageGenerations();
// Prints the number of characters for target string and the average number
// of generations required to get there for that number of characters.
// Prints out from server logs so it's easy to copy and paste to *.csv file.
console.log(targetLength + ", " + averageGenerations);
}