Skip to content

Updates to random number generation APIs #143710

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

joshtriplett
Copy link
Member

Updates based on discussions about random number generation.

  • Add comment on RandomSource::fill_bytes about multiple calls, to allow
    efficient implementations for random sources that generate a word at a time.

  • Drop the Random trait in favor of Distribution<T>, which will let people
    make calls like random(1..=6), and which allows for future expansion to
    non-uniform distributions, as well as floating-point. (For now, this is only
    implemented for RangeFull, to get the interface in place. Subsequent PRs
    will implement it for other range types.)

This allows efficient implementations for random sources that generate a
word at a time.
@rustbot
Copy link
Collaborator

rustbot commented Jul 9, 2025

r? @tgross35

rustbot has assigned @tgross35.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jul 9, 2025
@joshtriplett
Copy link
Member Author

r? @Amanieu

@rustbot rustbot assigned Amanieu and unassigned tgross35 Jul 9, 2025
@rust-log-analyzer

This comment has been minimized.

@Amanieu
Copy link
Member

Amanieu commented Jul 9, 2025

r=me once CI passes

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

This will let people make calls like random(1..=6), and allows for
future expansion to non-uniform distributions, as well as
floating-point.

For now, this is only implemented for `RangeFull`, to get the interface
in place. Subsequent commits will implement it for other range types.
@rust-log-analyzer

This comment has been minimized.

@rustbot
Copy link
Collaborator

rustbot commented Jul 11, 2025

The Miri subtree was changed

cc @rust-lang/miri

@joshtriplett
Copy link
Member Author

@bors r=Amanieu

@bors
Copy link
Collaborator

bors commented Jul 11, 2025

📌 Commit 900aa00 has been approved by Amanieu

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 11, 2025
@joshtriplett
Copy link
Member Author

@bors rollup

jhpratt added a commit to jhpratt/rust that referenced this pull request Jul 12, 2025
…nieu

Updates to random number generation APIs

Updates based on discussions about random number generation.

- Add comment on `RandomSource::fill_bytes` about multiple calls, to allow
  efficient implementations for random sources that generate a word at a time.

- Drop the `Random` trait in favor of `Distribution<T>`, which will let people
  make calls like random(1..=6), and which allows for future expansion to
  non-uniform distributions, as well as floating-point. (For now, this is only
  implemented for `RangeFull`, to get the interface in place. Subsequent PRs
  will implement it for other range types.)
bors added a commit that referenced this pull request Jul 12, 2025
Rollup of 15 pull requests

Successful merges:

 - #143554 (slice: Mark `rotate_left`, `rotate_right` unstably const)
 - #143634 (interpret/allocation: expose init + write_wildcards on a range)
 - #143710 (Updates to random number generation APIs)
 - #143774 (constify `From` and `Into`)
 - #143776 (std: move NuttX to use arc4random for random number generation)
 - #143778 (Some const_trait_impl test cleanups)
 - #143782 (Disambiguate between rustc vs std having debug assertions in `run-make-support` and `run-make` tests)
 - #143791 (Update sysinfo version to `0.36.0`)
 - #143796 (Fix ICE for parsed attributes with longer path not handled by CheckAttribute)
 - #143798 (Remove format short command trait)
 - #143803 (New tracking issues for const_ops and const_cmp)
 - #143814 (htmldocck: better error messages for some negative directives)
 - #143817 (Access `wasi_sdk_path` instead of reading environment variable in bootstrap)
 - #143822 (./x test miri: fix cleaning the miri_ui directory)
 - #143823 ([COMPILETEST-UNTANGLE 5/N] Test mode adjustments and other assorted cleanups)

r? `@ghost`
`@rustbot` modify labels: rollup
@jieyouxu
Copy link
Member

Failed in rollup (#143853 (comment)):

warning: dropping unsupported crate type `dylib` for target `x86_64-fortanix-unknown-sgx`

error[E0432]: unresolved import `crate::random::Random`
 --> library/std/src/sys/pal/sgx/abi/usercalls/mod.rs:5:42
  |
5 | use crate::random::{DefaultRandomSource, Random};
  |                                          ^^^^^^
  |                                          |
  |                                          no `Random` in `random`
  |                                          help: a similar name exists in the module: `random`

error[E0599]: no function or associated item named `random` found for type `i64` in the current scope
   --> library/std/src/sys/pal/sgx/abi/usercalls/mod.rs:182:34
    |
182 |             let deviation = i64::random(&mut DefaultRandomSource).checked_rem(tenth).unwrap_or(0);
    |                                  ^^^^^^ function or associated item not found in `i64`

@bors r-

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jul 13, 2025
@rustbot rustbot added the O-SGX Target: SGX label Jul 13, 2025
@joshtriplett
Copy link
Member Author

@bors r+ rollup

@bors
Copy link
Collaborator

bors commented Jul 13, 2025

📌 Commit 377aa76 has been approved by joshtriplett

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
O-SGX Target: SGX S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants