function getRandomFromSeed(seed){
//Initialize typed arrays for bit manipulation
var float=new Float64Array(1),
bytes=new Uint8Array(float.buffer); //8 bytes to represent 64 bits
float[0]=seed;
//Convert the seed's bits into a BigInt
var seedBigInt=0n;
for(let i=0;i<8;i++)
seedBigInt|=BigInt(bytes[i])<<BigInt(8*i);
//Apply the SplitMix64 algorithm
var x=(seedBigInt+0x9E3779B97F4A7C15n)&0xFFFFFFFFFFFFFFFFn,
z=x;
z=((z^(z>>30n))*0xBF58476D1CE4E5B9n)&0xFFFFFFFFFFFFFFFFn;
z=((z^(z>>27n))0x94D049BB133111EBn)&0xFFFFFFFFFFFFFFFFn;
z^=z>>31n;
//Extract the upper 53 bits to fit into a double's mantissa
var result_bits=z>>11n;
//Construct a double in the range [1, 2)
var sign_bit=0n,
exponent_bits=0x3FEn, //Exponent for 2^-1 (since bias is 1023)
fraction_bits=result_bits&0xFFFFFFFFFFFFFn, //Lower 52 bits
//Combine the sign, exponent, and fraction bits
double_bits=(sign_bit<<63n)|(exponent_bits<<52n)|fraction_bits;
//Write the bits back into the float's bytes
for(let i=0;i<8;i++)
bytes[i]=Number((double_bits>>BigInt(8i))&0xFFn);
//Subtract 1 to get a value in [0, 1)
return float[0]-1;
}
Your code is supposed to give values within the range [0,1) but it gives [-.5,0) instead, please fix.
Assistant
The issue you're experiencing arises from the exponent bits used when reconstructing the double-precision floating-point number. In your code, you're setting the exponent bits to 0x3FE, which corresponds to an exponent of -1 in the IEEE 754 double-precision format. This results in values in the range [0.5, 1) before subtracting 1, leading to a final range of [-0.5, 0).
To fix this and achieve the desired output in the range [0, 1), you should set the exponent bits to 0x3FF, which corresponds to an exponent of 0. This change ensures that the constructed double-precision number will be in the range [1, 2) before subtracting 1, resulting in a final range of [0, 1) after subtraction.
Here's the corrected code snippet:
javascript
// Construct a double in the range [1, 2)
var sign_bit = 0n,
exponent_bits = 0x3FFn, // Exponent for 2^0 (since bias is 1023)
fraction_bits = result_bits & 0xFFFFFFFFFFFFFn; // Lower 52 bits
// Combine the sign, exponent, and fraction bits
double_bits = (sign_bit << 63n) | (exponent_bits << 52n) | fraction_bits;
By updating the exponent_bits to 0x3FFn, the double-precision floating-point number (float[0]) will correctly represent a value in the range [1, 2). When you subtract 1, the final result will be in the desired range [0, 1).
Full corrected function:
javascript
function getRandomFromSeed(seed) {
// Initialize typed arrays for bit manipulation
var float = new Float64Array(1),
bytes = new Uint8Array(float.buffer); // 8 bytes to represent 64 bits
float[0] = seed;
// Convert the seed's bits into a BigInt
var seedBigInt = 0n;
for (let i = 0; i < 8; i++)
seedBigInt |= BigInt(bytes[i]) << BigInt(8 * i);
// Apply the SplitMix64 algorithm
var x = (seedBigInt + 0x9E3779B97F4A7C15n) & 0xFFFFFFFFFFFFFFFFn,
z = x;
z = ((z ^ (z >> 30n)) * 0xBF58476D1CE4E5B9n) & 0xFFFFFFFFFFFFFFFFn;
z = ((z ^ (z >> 27n)) * 0x94D049BB133111EBn) & 0xFFFFFFFFFFFFFFFFn;
z ^= z >> 31n;
// Extract the upper 53 bits to fit into a double's mantissa
var result_bits = z >> 11n;
// Construct a double in the range [1, 2)
var sign_bit = 0n,
exponent_bits = 0x3FFn, // Exponent for 2^0 (since bias is 1023)
fraction_bits = result_bits & 0xFFFFFFFFFFFFFn; // Lower 52 bits
// Combine the sign, exponent, and fraction bits
var double_bits = (sign_bit << 63n) | (exponent_bits << 52n) | fraction_bits;
// Write the bits back into the float's bytes
for (let i = 0; i < 8; i++)
bytes[i] = Number((double_bits >> BigInt(8 * i)) & 0xFFn);
// Subtract 1 to get a value in [0, 1)
return float[0] - 1;
}
Explanation:
Exponent Bits Correction: The exponent bias for double-precision floats is 1023. By setting exponent_bits to 0x3FFn (which is 1023 in decimal), the actual exponent becomes 0 (exponent_bits - 1023), placing the number in the range [1, 2).
Previous Exponent Setting: Previously, exponent_bits was set to 0x3FEn (which is 1022 in decimal). This resulted in an actual exponent of -1 (1022 - 1023), and thus the number was in the range [0.5, 1) before subtracting 1.
By making this adjustment, the function will now correctly produce values in the range [0, 1) as intended.