Skip to content

Instantly share code, notes, and snippets.

@henvic
Created November 21, 2015 20:04
Show Gist options
  • Save henvic/409bd08bf28cba38a862 to your computer and use it in GitHub Desktop.
Save henvic/409bd08bf28cba38a862 to your computer and use it in GitHub Desktop.
Math.random tests
1000000 iterations. Array is reset after first duplicate value.
length: 79775; duplicate: 14631346333771944
length: 45242; duplicate: 16620092559605836
length: 55295; duplicate: 9427159884944558
length: 123993; duplicate: 7038418645970523
length: 15575; duplicate: 8662485890090466
length: 96884; duplicate: 5140431500039995
length: 106546; duplicate: 40411726920865470
length: 54615; duplicate: 9114022604189812
length: 31491; duplicate: 9443272105418146
length: 86560; duplicate: 8835601878818125
length: 83472; duplicate: 19356185710057616
length: 38771; duplicate: 7487381640821695
length: 71672; duplicate: 9835371214430780
test: 30912.631ms
After 1000000 iterations found duplicates at positions:
106546, 123993, 15575, 31491, 38771, 45242, 54615, 55295, 71672, 79775, 83472, 86560, 96884
var results = [];
var foundAtPosition = [];
var total = 0;
var iterations = 1000000;
console.log(iterations + ' iterations. Array is reset after first duplicate value.');
function test() {
return Number(Math.random().toString().substr(2));
}
console.time('test');
while(total !== iterations) {
var n = test();
if (results.indexOf(n) !== -1) {
foundAtPosition.push(results.length);
console.log('length: ' + results.length + '; duplicate: ' + n);
results = [];
}
results.push(n);
total += 1;
}
console.timeEnd('test');
console.log('After ' + iterations + ' iterations found duplicates at positions:');
console.log(foundAtPosition.sort().join(', '));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment