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

Add functions to generate random u32 and u64 values #544

Merged
merged 2 commits into from
Nov 26, 2024
Merged

Conversation

newpavlov
Copy link
Member

@newpavlov newpavlov commented Nov 25, 2024

These functions can be helpful for seed generation and implementation of OsRng. Additionally, some backends (Hermit, RDRAND, RNDR, WASI p2) can directly generate random u32/u64 values. Relying on the byte API may be less efficient in these cases.

Using u32 and u64 as function names may seem problematic, but based on the fastrand experience, it works well in practice, provided that users reference them as getrandom::u32/u64 without importing them directly.

@newpavlov newpavlov requested a review from josephlr November 25, 2024 13:37
@newpavlov
Copy link
Member Author

cc @dhardy @briansmith

@dhardy
Copy link
Member

dhardy commented Nov 25, 2024

@newpavlov maybe though not really important for rand_core (we can already convert from bits). In any case, I won't depend on this until a new release is out so don't expect it to see use in rand v0.9.0.

@newpavlov
Copy link
Member Author

@dhardy
My plan is to release getrandom v0.3 before before rand v0.9. You will not be able to silently migrate from v0.2 to v0.3 even with rust-random/rand#1537 because of the changes around optional backend handling.

@dhardy dhardy mentioned this pull request Nov 25, 2024
Copy link
Member

@dhardy dhardy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So IIUC the purpose of the extra API is to:

  • Avoid an allocation when using WASI P2. I'd be tempted just to test whether dest.len() <= 8 (or some larger threshold) and use get_random_u64 then... except you always use that anyway. (Is this approach sensible for large buffers anyway?)
  • Minor optimisations for RDRAND/RNDR

To me, this doesn't seem to be sufficient reason to introduce the extra API surface.

@newpavlov
Copy link
Member Author

newpavlov commented Nov 25, 2024

The main motivation is user convenience, since the main use case of this crate is seed generation. The u32/u64 generation support by some platforms is just an additional argument for why it should be done in this crate.

@dhardy
Copy link
Member

dhardy commented Nov 25, 2024

The main motivation is user convenience, since the main use case of this crate is seed generation.

I always saw this crate as a low level implementation crate, not something which would be used directly by many users.

getrandom does now have 1093 published dependent crates to rand's 16269, so maybe I'm partly wrong.

@newpavlov newpavlov merged commit e694075 into master Nov 26, 2024
57 checks passed
@newpavlov newpavlov deleted the u32_u64 branch November 26, 2024 13:38
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

Successfully merging this pull request may close these issues.

2 participants