JSFiddle - React, Tailwind, and code Playground
HTML
Nodes:<input type="number" id="N" value="65536" />
Tries:<input type="number" id="I" value="8" />
<input type="button" id="B" value="Gossip" />
<output></output>
JavaScript
function gossip(nodes, tries, startNode, reached) {
var stack = [startNode, tries];
while(stack.length > 0) {
var ttl = stack.pop();
var n = stack.pop();
reached[n] = 1;
if(ttl <= 0) { continue; }
for(var i=0; i < ttl; i++) {
stack.push(Math.floor(Math.random() * nodes), ttl - 1);
}
}
return reached;
}
function runIt(nodes, tries) {
var result = gossip(nodes, tries, Math.floor(Math.random() * nodes), {});
// count
var count = Object.keys(result).length;
document.querySelector('output').innerHTML += '<br/>' + count + ' ' + (count / nodes)*100 + '%';
}
document.querySelector('#B').onclick = function(e) {
runIt(parseInt(document.querySelector('#N').value), parseInt(document.querySelector('#I').value));
};