Write a uniformly-distributed double floating-point pseudo-random generator within the range [0,1) that has a (double floating-point) seed as its parameter, always returning the same value when passing in the same seed, but exhibits drastic changes even when a single mantissa bit of the seed has been altered, using 64-bit IEEE 754 double values and var float=new Float64Array(1),bytes=new Uint8Array(float.buffer);float[0]=value; to manipulate double-value bits in JavaScript. Use statistical tests to evaluate its uniformity, bit-level analysis, and periodicity check to prevent repetition.
Assistant
To create a uniformly distributed pseudo-random number generator that meets the specified criteria, we'll implement a function that takes a double-precision floating-point seed and produces a double in the range [0, 1). The generator will:
Return the same value for the same seed: It's deterministic based on the seed.
Exhibit drastic changes with slight seed alterations: Even flipping a single bit in the seed will significantly change the output.
Use IEEE 754 double values and bit manipulation in JavaScript: We'll manipulate the bits of the double-precision floating-point numbers using typed arrays.
We'll use the SplitMix64 algorithm, a statistically sound pseudo-random number generator with good avalanche properties—changing one bit of the input changes many bits of the output.
Here's the implementation:
javascript
function getRandomFromSeed(seed) {
// Initialize typed arrays for bit manipulation
var float = new Float64Array(1);
var 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 (var i = 0; i < 8; i++) {
seedBigInt |= BigInt(bytes[i]) << BigInt(8 * i);
}
// Apply the SplitMix64 algorithm
var x = (seedBigInt + 0x9E3779B97F4A7C15n) & 0xFFFFFFFFFFFFFFFFn;
var 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;
var exponent_bits = 0x3FEn; // Exponent for 2^-1 (since bias is 1023)
var 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 (var i = 0; i < 8; i++) {
bytes[i] = Number((double_bits >> BigInt(8 * i)) & 0xFFn);
}
// Subtract 1 to get a value in [0, 1)
var result = float[0] - 1.0;
return result;
}
Explanation:
Bit Manipulation of the Seed: We start by converting the seed (a double) into its raw 64-bit representation using typed arrays. This allows us to access and manipulate the individual bits of the seed.
Applying SplitMix64: We use the SplitMix64 algorithm, which involves several bitwise operations and multiplications with large constants. This algorithm has good avalanche properties, ensuring that tiny changes to the input (like flipping a single bit) result in significant changes to the output.
Constructing the Double in [0, 1):
We extract the upper 53 bits of the 64-bit output from SplitMix64, as JavaScript numbers (IEEE 754 doubles) have a precision of 53 bits.
We set the exponent bits to represent an exponent of -1, ensuring the resulting double is in the range [1, 2).
The fraction (mantissa) bits are set to the extracted bits from the random output.
By combining the sign, exponent, and fraction bits, we reconstruct a new double-precision floating-point number.
Subtracting 1 from this number scales it down to the [0, 1) range.
Statistical Testing and Analysis:
To ensure the generator's uniformity and randomness, you should:
Perform statistical tests like the Kolmogorov-Smirnov test to compare the distribution of generated numbers to a uniform distribution.
Analyze the bit-level output to check for patterns or biases.
Check for periodicity by generating a large sequence of numbers and ensuring that repetitions occur only after the expected period (which is very large with SplitMix64).
By following this approach, we've created a pseudo-random number generator in JavaScript that:
Is deterministic and seed-based.
Responds sensitively to small changes in the seed.
Produces uniformly distributed numbers in [0, 1) with good statistical properties.