Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Random Seed distribution tendency #1

Open
nbikos opened this issue May 30, 2024 · 1 comment
Open

Random Seed distribution tendency #1

nbikos opened this issue May 30, 2024 · 1 comment

Comments

@nbikos
Copy link

nbikos commented May 30, 2024

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
Copy link
Author

nbikos commented May 30, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant