Created
July 2, 2014 17:02
-
-
Save dminkovsky/9214d562ed903a0151e5 to your computer and use it in GitHub Desktop.
64-bit random numbers in Node
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var bignum = require('bignum'); | |
var crypto = require('crypto'); | |
var randomBignum = function() { return bignum.fromBuffer(crypto.pseudoRandomBytes(8)); } | |
var avg = randomBignum(); | |
var avg_len = avg.bitLength(); | |
var lengths = {}; | |
for (var i = 0; i < 1e6; i++) { | |
var big = randomBignum(); | |
avg = avg.add(big).div(2); | |
var length = big.bitLength(); | |
if (typeof lengths[length] === 'undefined') { lengths[length] = 0; } | |
lengths[length] += 1; | |
avg_len = (avg_len + length) / 2; | |
} | |
console.log('avg %s', avg); | |
console.log('avg len %s', avg_len); | |
console.info('lens %s', require('util').inspect(lengths)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dmitry@airosol /private/tmp $ time node big.js | |
avg 8386866841744540769 | |
avg len 63.11672078688376 | |
lens { '45': 1, | |
'47': 2, | |
'48': 7, | |
'49': 15, | |
'50': 23, | |
'51': 66, | |
'52': 114, | |
'53': 248, | |
'54': 521, | |
'55': 955, | |
'56': 1905, | |
'57': 4019, | |
'58': 7742, | |
'59': 15552, | |
'60': 31396, | |
'61': 63048, | |
'62': 125182, | |
'63': 249271, | |
'64': 499933 } | |
real 0m50.670s | |
user 0m48.681s | |
sys 0m0.690s |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dmitry@airosol /private/tmp $ time node big.js | |
avg 12648992876624866288 | |
avg len 63.93897900578206 | |
lens { '44': 2, | |
'46': 1, | |
'47': 3, | |
'48': 12, | |
'49': 10, | |
'50': 40, | |
'51': 56, | |
'52': 127, | |
'53': 245, | |
'54': 503, | |
'55': 931, | |
'56': 1928, | |
'57': 3817, | |
'58': 7845, | |
'59': 15665, | |
'60': 31260, | |
'61': 62168, | |
'62': 125822, | |
'63': 250385, | |
'64': 499180 } | |
real 0m51.208s | |
user 0m49.258s | |
sys 0m0.698s |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dmitry@airosol /private/tmp $ time node big.js | |
avg 11604449224001379105 | |
avg len 63.694875686108915 | |
lens { '43': 1, | |
'44': 1, | |
'45': 1, | |
'46': 1, | |
'47': 5, | |
'48': 5, | |
'49': 14, | |
'50': 25, | |
'51': 69, | |
'52': 116, | |
'53': 242, | |
'54': 478, | |
'55': 938, | |
'56': 1992, | |
'57': 4013, | |
'58': 7845, | |
'59': 15558, | |
'60': 31338, | |
'61': 62557, | |
'62': 125616, | |
'63': 250079, | |
'64': 499106 } | |
real 0m50.762s | |
user 0m48.705s | |
sys 0m0.716s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment