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

Correct an issue in foundry.utils.randomID with double-weight on the "9" character. #7877

Closed
6 tasks done
ghost91- opened this issue Aug 20, 2022 · 0 comments
Closed
6 tasks done
Assignees
Labels
api Issues related to the API used by Mod Devs bug Functionality which is not working as intended

Comments

@ghost91-
Copy link

ghost91- commented Aug 20, 2022

What happened?

foundry.utils.randomID is documented to generate IDs of the length given as parameter. However, it always creates IDs of length 16.

Additionally, the possible characters contain 9 twice, so 9 is slightly more likely in the generated ids than any other character.

The current implementation is

export function randomID(length=16) {
  const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz09123456789";
  const r = Array.from({length: 16}, () => (Math.random() * 63) >> 0);
  return r.map(i => chars[i]).join("");
}

but it should probably be something like

export function randomID(length=16) {
  const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  const r = Array.from({length}, () => (Math.random() * chars.length) >> 0);
  return r.map(i => chars[i]).join("");
}

instead.

What ways of accessing Foundry can you encounter this issue in?

  • Native App (Electron)
  • Chrome
  • Firefox
  • Safari
  • Other

Reproduction Steps

  • Call foundry.utils.randomID(1)
  • Observe that the result is a string with 16 characters

What core version are you reporting this for?

Version 10 Testing 4 (build 278)

Relevant log output

No response

Bug Checklist

  • The issue occurs while all Modules are disabled
@ghost91- ghost91- added the bug Functionality which is not working as intended label Aug 20, 2022
@aaclayton aaclayton added the api Issues related to the API used by Mod Devs label Aug 20, 2022
@aaclayton aaclayton added this to the Version 10 - Testing 5 milestone Aug 20, 2022
@aaclayton aaclayton self-assigned this Aug 20, 2022
@aaclayton aaclayton changed the title foundry.utils.randomID ignores the length parameter and 9 is slightly more likely than any other character Correct an issue in foundry.utils.randomID with double-weight on the "9" character. Aug 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api Issues related to the API used by Mod Devs bug Functionality which is not working as intended
Projects
No open projects
Status: Done
Development

No branches or pull requests

2 participants