Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
unbiased ranged random integer generation functions
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/28.0 Chrome/130.0.0.0 Mobile Safari/537.36
Browser:
Chrome Mobile 130
Operating system:
Android
Device Platform:
Mobile
Date tested:
10 months ago
Test name
Executions per second
Classic 16
300936.2 Ops/sec
Mul shf 16
268286.3 Ops/sec
Imul shf 16
267976.9 Ops/sec
Classic 32
276929.8 Ops/sec
Mul shf bigint
189709.0 Ops/sec
Mul shf 32
123792.7 Ops/sec
Script Preparation code:
const ta16 = new Uint16Array(1); const ta32 = new Uint32Array(1); const bound = 13000;
Tests:
Classic 16
if ((bound & -bound) === bound) { window.crypto.getRandomValues(ta16); return bound * ta16[0] >>> 16; } const limit = 65536 - 65536 % bound; do { window.crypto.getRandomValues(ta16); } while (ta16[0] >= limit); return ta16[0] % bound;
Mul shf 16
if ((bound & -bound) === bound) { window.crypto.getRandomValues(ta16); return bound * ta16[0] >>> 16; } const limit = (-bound >>> 0) % bound; let value; do { window.crypto.getRandomValues(ta16); value = ta16[0] * bound; } while ((value & 65535) < limit); return value >>> 16;
Imul shf 16
if ((bound & -bound) === bound) { window.crypto.getRandomValues(ta16); return bound * ta16[0] >>> 16; } const limit = (-bound >>> 0) % bound; let value; do { window.crypto.getRandomValues(ta16); value = Math.imul(ta16[0], bound); } while ((value & 65535) < limit); return value >>> 16;
Classic int
if ((bound & -bound) === bound) { window.crypto.getRandomValues(ta32); return ta32[0] * bound / 4294967296 >>> 0; } const limit = 4294967296 - 4294967296 % bound; do { window.crypto.getRandomValues(ta32); } while (ta32[0] >= limit); return ta32[0] % bound;
Mul shf bigint
if ((bound & -bound) === bound) { window.crypto.getRandomValues(ta32); return ta32[0] * bound / 4294967296 >>> 0; } const limit = (-bound >>> 0) % bound; let bits, mLow; let mHigh; do { window.crypto.getRandomValues(ta32); bits = ta32[0] >>> 1; const m = BigInt(bits) * BigInt(bound); mLow = Number(m & 2147483647n); mHigh = Number(m >> 31n); } while (mLow < limit); return mHigh;
Mul shf 32
if ((bound & -bound) === bound) { window.crypto.getRandomValues(ta32); return ta32[0] * bound / 4294967296 >>> 0; } const limit = (-bound >>> 0) % bound; let value; do { window.crypto.getRandomValues(ta32); value = ta32[0] * bound; } while ((value & 4294967295) % bound < limit); return value / 4294967296 >>> 0;
java nextInt16(bound)
if ((bound & -bound) === bound) { crypto.getRandomValues(ta16); return bound * ta16[0] >>> 16; } let value; do { crypto.getRandomValues(ta16); value = ta16[0] % bound; } while (ta16[0] - value + (bound - 1) < 0); return value;
java nextInt32(bound)
if ((bound & -bound) === bound) { window.crypto.getRandomValues(ta32); return ta32[0] * bound / 4294967296 >>> 0; } let bits, value; do { window.crypto.getRandomValues(ta32); bits = ta32[0] >>> 1; value = bits % bound; } while (bits - value + (bound - 1) < 0); return value;