maciej-trebacz / ff7-speed-square

App for FF7 speedrunners to fix issues with the game
0 stars 1 forks source link

Random Seed distribution tendency #1

Open nbikos opened 3 months ago

nbikos commented 3 months ago

Hi,

Thank you for making and maintaining this tool, making the speedrun so approachable!

In reviewing the logic that generates random seeds, I believe there is an issue with the distribution created. https://github.com/maciej-trebacz/ff7-speed-square/blob/master/src/ff7.ts#L120-L129

  getRandomSeed() {
    // Generate a random seed that's at least equal to Jan 1, 1980 and is capped at 2^31 - 1
    let rng = 0;
    while (rng < 315529200) {
      const arr = new Uint32Array(1);
      webcrypto.getRandomValues(arr);
      rng = arr[0] % (2 ** 31 - 1);
    }
    return rng;
  }

The goal I believe is to produce a uniform distribution with a minimum of Jan 1, 1980 in unix epoch seconds up to 2^31-1. The application of modulus to limit the maximum reduces uniformity of the results because the largest number can be 2^32. If the number generated is between that 2^31 and 2^32 range, it will be aliased back onto a real seed toward the lower end because the remainder is taken.

The end result is the range of seeds randomly produced is not uniform.

Separately, the comment suggests the minimum should be Jan 1, 1980 00:00:00, but the effective minimum is Dec 31, 1979 23:00:00. This only impacts the distribution created if the seed between 23:00:00 and 00:00:00 are aliased back over the valid set. Happy to open a separate issue about the minimum and valid seeds on the low range specifically if desired!

nbikos commented 3 months ago

I did not note any contribution guidelines, but I am happy to propose a PR that would throw out random numbers > 2^31-1 and generate again until finding a compliant seed.