Skip to content

Seeded generators are not random! #82

@CJxD

Description

@CJxD

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

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions