Skip to content

Instantly share code, notes, and snippets.

@TechnotronicOz
Created October 2, 2014 16:36
Show Gist options
  • Select an option

  • Save TechnotronicOz/5f329401877fc2bbd817 to your computer and use it in GitHub Desktop.

Select an option

Save TechnotronicOz/5f329401877fc2bbd817 to your computer and use it in GitHub Desktop.
'use strict';
var uuid = require('node-uuid'),
_ = require('lodash');
var runCount = 1000000,
time = process.hrtime(),
inc = -1,
resultsArr = [],
uniqueArr = [],
truths = 0,
segment = { high: 50, low: 30 };
console.log('target population: ', ((segment.high - segment.low) / 100));
while (++inc < runCount) {
var id = uuid.v4(),
chars = parseChars(id),
mod = modSegment(chars, segment);
if (mod.inSegment) {
truths++;
}
resultsArr.push(mod);
uniqueArr.push(id);
}
var percentTrue = truths / runCount;
console.info('percent inSegment true: ', percentTrue);
console.info('all unique?', (_.uniq(uniqueArr).length === runCount));
var diff = process.hrtime(time);
console.info("Execution time (hr): %ds %dms", diff[0], diff[1]/1000000);
/* Convert letters to their char code in the uuid string */
function parseChars(uuid) {
var code = [],
a = 0;
while (a < uuid.length) {
isNaN(uuid[a]) ? code.push(uuid.charCodeAt(a)) : code.push(uuid[a]);
a++;
}
return code.join('');
}
/* Find the modulus the uuid modified string lives within and it's segment */
function modSegment(num, segment) {
var cookieMod = (num / 2) % 100;
return {
cookieMod: cookieMod,
inSegment: (cookieMod <= segment.high) && (cookieMod >= segment.low)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment