Skip to content

Instantly share code, notes, and snippets.

@toddbranch
Created December 8, 2015 14:57
Show Gist options
  • Save toddbranch/e881e82bb8d30fca800b to your computer and use it in GitHub Desktop.
Save toddbranch/e881e82bb8d30fca800b to your computer and use it in GitHub Desktop.
randomFromStream
function randomFromStream(currentResult, length) {
var potentialResult = gfs();
// no more numbers in stream
if (potentialResult === null) {
return currentResult;
}
length += 1;
if (Math.floor(Math.random() * length === 0)) {
currentResult = potentialResult;
}
return randomFromStream(currentResult, length);
}
randomFromStream(null, 0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment