Created
February 8, 2020 15:49
-
-
Save spicydog/4dea19c60fa58aeff88909c8c6e91804 to your computer and use it in GitHub Desktop.
Central Limit Theorem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const times = 20; | |
const n = 5 | |
const set = { | |
1: 1, | |
2: 5, | |
3: 7, | |
4: 5, | |
5: 1, | |
} | |
function shuffle(a) { | |
let j, x, i; | |
for (i = a.length - 1; i > 0; i--) { | |
j = Math.floor(Math.random() * (i + 1)); | |
x = a[i]; | |
a[i] = a[j]; | |
a[j] = x; | |
} | |
return a; | |
} | |
function pickItems(spaces, n) { | |
let s = shuffle(spaces.slice()) | |
let pickedItems = []; | |
for (let i=0; i<n ;i++) { | |
let item = s.pop(); | |
pickedItems.push(item); | |
} | |
return pickedItems; | |
} | |
let t = "" | |
spaces = []; | |
for (let s in set) { | |
t = t + "\n" | |
for (i=0; i<set[s]; i++) { | |
spaces.push(s) | |
t = t + s + " " | |
} | |
} | |
console.log(t + "\n") | |
console.log("Times: " + times); | |
console.log("N: " + n); | |
console.log(spaces) | |
console.log("Number of items: " + spaces.length) | |
let results = [] | |
for (let i = 0; i < times; i++) { | |
pickedItems = pickItems(spaces, n) | |
results.push(pickedItems) | |
} | |
console.log("Final picking result: "); | |
console.log(results); | |
let totalItems = results.flat(); | |
console.log(totalItems); | |
let count = {}; | |
totalItems.forEach(function(i) { count[i] = (count[i]||0) + 1;}); | |
let output = "\n" | |
for (i in count) { | |
output += i + "\t" + count[i] + "\n"; | |
} | |
console.log(output); | |
output = "\n" | |
for (i in count) { | |
output += i + "\t" + count[i]/n + "\n"; | |
} | |
console.log(output); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment