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

Not just [0,1), but also random int #22

Closed
Artoria2e5 opened this issue May 11, 2024 · 5 comments
Closed

Not just [0,1), but also random int #22

Artoria2e5 opened this issue May 11, 2024 · 5 comments

Comments

@Artoria2e5
Copy link

Artoria2e5 commented May 11, 2024

In #19 (comment) I talked about how it might be useful to have a "correct dice roll" as a tangential. I still think that's important (people mess that up more commonly do you think), so I'm making a new issue.

Beyond just [0,1), I see potential use for a method for a random integer.

The .randomInt() method

To obtain a random integer number from a PRNG object, the object has a .randomInt() method. On each invocation, it will output an appropriate pseudo-random number based on its seed, and then update its seed for the next invocation.

  • When .randomInt() is invoked with zero arguments, it returns a random unsigned 32-bit integer in the range [0, 2^32-1].
  • When .randomInt() is invoked with one argument x, it returns a random integer in the range [0, h). x must be in the range [1, Number.MAX_SAFE_INTEGER].
  • When .randomInt() is invoked with two argument l, h, it returns a random integer in the range [l, h). h-l must be in the range [1, Number.MAX_SAFE_INTEGER].

Recommended implementation

See swiftlang/swift#39143.

Open questions

  • Do we return a number constrained to uint53 (MAX_SAFE_INTEGER), a 64-bit-or-whatever BigInt, or a number constrained to uint32?
@bakkot
Copy link

bakkot commented May 11, 2024

See https://github.com/tc39-transfer/proposal-random-functions.

@michaelficarra
Copy link
Member

Also duplicate of #21.

@Artoria2e5
Copy link
Author

Aye, will close.

@tabatkins
Copy link
Collaborator

Yup, the current intention is that the seeded PRNG will support .random(), acting identically to Math.random() (but seeded), and then additionally support any and all additional randomness methods the language grows. I don't intend to innovate in that regard in this proposal.

@Artoria2e5
Copy link
Author

Artoria2e5 commented May 16, 2024

My original motivation is that "64 bits of random" is more fundamental to an RNG than "[0,1) double", but I guess that alone doesn't mean it has to be in the same spec. After all everyone knows that.

The consideration around MAX_SAFE_INTEGER should maybe also get into your separate proposal. Or maybe we get a BigInt output, I still can't decide.

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

4 participants