Skip to content

Instantly share code, notes, and snippets.

View mumbleskates's full-sized avatar
🤔

Kent Ross mumbleskates

🤔
  • 🌵
View GitHub Profile
@mumbleskates
mumbleskates / random_k.js
Last active November 28, 2016 03:45 — forked from seejohnrun/random_k.js
Select (n) random elements from a weighted set randomly
// ported from: http://stackoverflow.com/questions/2140787/select-random-k-elements-from-a-list-whose-elements-have-weights
// each node in the heap has a value, weight, and totalWeight
// the totalWeight is the weight of the node plus any children
var Node = {};
var newNode = function (value, weight, totalWeight) {
var node = Object.create(Node);
node.value = value;
node.weight = weight;
node.totalWeight = totalWeight;