Data update
This commit is contained in:
parent
72eb4943cb
commit
4d5544505c
2347 changed files with 62432 additions and 16731 deletions
36
Task/Knuths-algorithm-S/JavaScript/knuths-algorithm-s.js
Normal file
36
Task/Knuths-algorithm-S/JavaScript/knuths-algorithm-s.js
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
class SOfN {
|
||||
constructor(n) {
|
||||
this.m = this.n = n
|
||||
this.s = []
|
||||
}
|
||||
|
||||
add(item) {
|
||||
if (this.s.length < this.n) {
|
||||
this.s.push(item)
|
||||
} else {
|
||||
const rand = Math.floor(Math.random() * ++this.m)
|
||||
if (rand < this.n) {
|
||||
this.s[rand] = item
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function main() {
|
||||
for (const [n, m] of [[3, 3], [3, 10]]) {
|
||||
const freqs = new Array(m).fill(0)
|
||||
|
||||
for (let i = 0; i < 1e5; ++i) {
|
||||
const sOfN = new SOfN(n)
|
||||
for (let x = 0; x < m; ++x) {
|
||||
sOfN.add(x)
|
||||
}
|
||||
for (const d of sOfN.s) {
|
||||
++freqs[d]
|
||||
}
|
||||
}
|
||||
console.log(`Results for n=${n}, m=${m}: [${freqs.join(', ')}]`)
|
||||
}
|
||||
}
|
||||
|
||||
main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue