-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
60 lines (49 loc) · 1.61 KB
/
Copy pathscript.js
File metadata and controls
60 lines (49 loc) · 1.61 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
/**
* number of bits
*/
const n = 16;
const iterationStep = 1000;
window.addEventListener("load", () => {
const yElement = document.getElementById("y");
const zElement = document.getElementById("z");
const xElement = document.getElementById("x");
const iterationCountElement = document.getElementById("iterations");
const probElement = document.getElementById("prob");
const upperRandomLimit = Math.pow(2, n);
let iterationCount = 0;
let equalCount = 0;
const loop = () => {
let y;
let z;
let x;
for (let i = 0; i < iterationStep; i++) {
y = Math.floor(Math.random() * upperRandomLimit);
z = Math.floor(Math.random() * upperRandomLimit);
if (y !== z) {
x = Math.floor(Math.random() * upperRandomLimit);
if (binaryVectorSkalarProduct(x, y) === binaryVectorSkalarProduct(x, z)) {
equalCount++;
}
iterationCount++;
} else {
// console.info(`numbers were same: ${y}`);
}
}
yElement.value = y.toString(2).padStart(n, "0");
zElement.value = z.toString(2).padStart(n, "0");
xElement.value = x.toString(2).padStart(n, "0");
iterationCountElement.value = iterationCount;
probElement.value = equalCount / iterationCount;
requestAnimationFrame(loop);
}
loop();
});
function binaryVectorSkalarProduct(x, y) {
let product = x & y;
let sum = 0;
while (product > 0) {
sum ^= product & 1;
product = product >> 1;
}
return sum
}