Skip to content

Commit

Permalink
Add distribution test
Browse files Browse the repository at this point in the history
  • Loading branch information
ai committed Jul 8, 2018
1 parent 8524b54 commit 0935ba4
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions test/distribution.js
@@ -0,0 +1,28 @@
var shortid = require('..')

var COUNT = 100 * 1000
var LENGTH = shortid().length

var chars = { }
for (var i = 0; i < COUNT; i++) {
var id = shortid()
for (var j = 0; j < id.length; j++) {
var char = id[j]
if (!chars[char]) chars[char] = 0
chars[char] += 1
}
}

var ALPHABET = Object.keys(chars).length

var d = Object
.keys(chars)
.map(i => [i, chars[i]])
.sort((a, b) => b[1] - a[1])
.map(i => {
var probability = i[1] * ALPHABET / (COUNT * LENGTH)
console.log(i[0] + ' ' + probability)
return probability
})

console.log('\nMax. difference: ' + (d[0] - d[d.length - 1]))

0 comments on commit 0935ba4

Please sign in to comment.