Skip to content

Instantly share code, notes, and snippets.

Keybase proof

I hereby claim:

  • I am michalkot on github.
  • I am mmk (https://keybase.io/mmk) on keybase.
  • I have a public key whose fingerprint is 30EB AEBC F0D2 AEA2 2244 AD95 46C4 EF7C C4A4 CE65

To claim this, I am signing this object:

@michalkot
michalkot / weightedRandom.js
Last active November 26, 2015 13:55
weightedRandom
function weightedRandom(ar) {
var ar = ar.sort(x => x.w);
var sum = ar.reduce((p, c) => p + c.w, 0);
// normalise
var soFar = 0;
ar.map(o => {
o.w = o.w / sum;
o.w += soFar;
soFar += o.w;
});
// Password generator bookmark.
// Fills in active text field OR all password fields if none is selected
// Run in chrome console, right click in bookmarks bar, select new page, paste in URL field
function passGen(n) {
let ar = new Uint32Array(n);
window.crypto.getRandomValues(ar);
ar = [...ar];
const pass = ar.map(n => n.toString(32)).join('-');
if (document.activeElement) document.activeElement.value = pass;
else [...document.querySelectorAll('input[type="password"]')].forEach(node => node.value = pass);