-
-
Notifications
You must be signed in to change notification settings - Fork 30
Open
Description
When a new PRNG is generated with random seeds, the first generated number is typically not random.
Here is a test case:
function makePrng(seed) {
const rand = new Random(seed);
return rand.uniform();
}
it('should produce non-overlapping uniform distributions for different seeds', () => {
// Generate random string seeds
const generateRandomString = () => {
return (
Math.random().toString(36).substring(2, 15) +
Math.random().toString(36).substring(2, 15)
);
};
const numSeeds = 100;
const seeds: string[] = [];
// Generate unique random string seeds
for (let i = 0; i < numSeeds; i++) {
seeds.push(generateRandomString());
}
// Get first random number from each seeded PRNG
const firstValues: number[] = seeds.map((seed) => makePrng(seed)());
// Check for collisions in first values
const uniqueValues = new Set(firstValues);
const collisionRate = 1 - uniqueValues.size / firstValues.length;
// With 100 seeds and uniform distribution over [0,1), collision rate should be near zero
// Allow up to 5% collision rate (should be much lower for good PRNG)
expect(collisionRate).toBeLessThan(0.05); // FAIL: typically ~0.36
});
Metadata
Metadata
Assignees
Labels
No labels