-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrsa.js
71 lines (62 loc) · 2.17 KB
/
rsa.js
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
const firstOgArray = document.getElementById("ogArray");
var table = document.getElementById("roundTable");
let nodeList = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0];
firstOgArray.innerHTML = nodeList;
// A function to get a random number within a limit
function getRandomNumber(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
// To keep track of rounds
let rounds = 0;
// To pass the rumour from spreader to spreadee
function passRumour(nodeList) {
// To check if the goal is achieved
if (!nodeList.includes(0)) {
console.log("Rumour passed successfully to all nodes :)");
return;
}
// Starting of a round
nodeList.forEach((element) => {
// Traversing through each element in the array
if (element == 1) {
let random = getRandomNumber(0, 9);
// When the element is 1, it will perform the choosing of a random element and changing it into 1
console.log("Spreader Element chosen!");
if (nodeList[random] == 1) {
// Non spreadee - Chose an element which is already 1
console.log("Random index", random);
console.log("Bad Event");
} else if (nodeList[random] == 0) {
// Spreadee chosen! - Chose a 0 and changing it into 2
nodeList.splice(random, 1, 2);
console.log("Random index", random);
console.log("Good Event!", nodeList);
} else {
console.log("Crash node chosen ", nodeList[random]);
}
}
});
rounds++;
var newRow = table.insertRow(table.rows.length);
var cell1 = newRow.insertCell(0);
var cell2 = newRow.insertCell(1);
cell1.innerHTML = rounds;
console.log("END OF ROUND ", rounds);
// Changing 2s to 1s (basically spreaders to spreadees for the next round)
for (i = 1; i < nodeList.length; i++) {
if (nodeList[i] == 2) {
nodeList.splice(i, 1, 1);
}
}
console.log("NODELIST", nodeList);
cell2.innerHTML = nodeList;
// CRASH NODES REPRESENTED BY "C"
// ONE THIRD OF THE NODES CRASH AFTER THE FIRST ROUND
if (rounds == 1) {
nodeList.splice(0, 3, "C", "C", "C");
console.log("Array after crash nodes", nodeList);
}
passRumour(nodeList);
}
// Calling the function for the first time
passRumour(nodeList);