Round 1. Keep going through Hamlet, adding new words as you go. If you come to a word that’s already on your list, flip a coin again. If it’s tails, delete the word; heads, and the word stays on the list.
To: Round 1. Keep going through Hamlet, but now flipping a coin for each word. If it’s tails, delete the word if it exists; heads, and add the word if it's not already on the list.
Old edit: Round 1. Keep going through Hamlet, adding words but now flipping a coin immediately after adding it. If it’s tails, delete the word; heads, and the word stays on the list. function generateRandomNumbers(c, n) {
let randomNumbers = new Array(c);
for (let i = 0; i < randomNumbers.length; i++) {
let randomNumber = Math.floor(Math.random() * (n + 1));
randomNumbers[i] = randomNumber;
}
return randomNumbers;
}
function run(w, wS, m, r) {
function round(r) {
while(wS.size < m) {
const next = w.next()
if (next.done) return true;
wS.add(next.value)
prune(next.value, r)
}
return false
}
function prune(v,r) {
for (let i = 0; i < r; i++) {
const flip = new Boolean(Math.round(Math.random()))
if (flip == false) {
wS.delete(v)
}
}
}
function purge(wS) {
const copy = new Set(wS)
copy.forEach(ith=>{
const flip = new Boolean(Math.round(Math.random()))
if (flip == false) {
wS.delete(ith)
}
})
}
const done = round(r);
if (!done) {
purge(wS)
return run(w, wS, r+1,m)
}
console.log(`Round ${r} done. ${wS.size} Estimate: ${wS.size / (1/Math.pow(2,r))}`)
}
const memory = 1000
const words = generateRandomNumbers(3000000,15000)
const w = words[Symbol.iterator]() // create an iterator
const wS = new Set();
run(w,wS, memory,0);
Should be changed to: