-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgenerator.js
More file actions
75 lines (60 loc) · 1.85 KB
/
Copy pathgenerator.js
File metadata and controls
75 lines (60 loc) · 1.85 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
const n_chars = 10;
const n_sequence = 20;
const n_pairs = 100;
const all_characters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
function randomRange(min, max) {
const diff = max - min;
const idx = Math.floor(Math.random() * diff);
return min + idx;
}
function randomValue(array) {
return array[randomRange(0, array.length)];
}
var characterMap = {};
while (Object.keys(characterMap).length < n_chars) {
const ch = randomValue(all_characters)
characterMap[ch] = 1;
}
const characters = Object.keys(characterMap);
// console.log("characters:", JSON.stringify(characters));
var pairs = {};
function recurse(pairs, max, ch1, ch2) {
if (Object.keys(pairs).length >= max) { return; }
const ch3 = randomValue(characters);
const k0 = ch1 + ch2;
if (pairs.hasOwnProperty(k0)) {
return;
}
const k1 = ch1 + ch3;
const k2 = ch3 + ch2;
pairs[k0] = ch3;
if (!pairs.hasOwnProperty(k1)) {
recurse(pairs, max, ch1, ch3);
}
if (!pairs.hasOwnProperty(k2)) {
recurse(pairs, max, ch3, ch2);
}
}
while (Object.keys(pairs).length < n_pairs) {
const ch1 = randomValue(characters);
const ch2 = randomValue(characters);
recurse(pairs, n_pairs, ch1, ch2);
}
// console.log("--------------------------------------------");
// console.log("count:", Object.keys(pairs).length);
// console.log("pairs:", JSON.stringify(pairs));
const keys = Object.keys(pairs);
var sequence = "";
for (var i = 0; i < n_sequence/2; ++i) {
const key = randomValue(keys);
sequence += key;
}
// console.log(sequence);
document.open();
document.write(sequence);
document.write('<br>');
for (const [key, value] of Object.entries(pairs)) {
document.write(key, ' -> ', value);
document.write('<br>');
}
document.close();