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

Implement Random for num::{Saturating,Wrapping} #136733

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

Conversation

sorairolake
Copy link
Contributor

rust-lang/libs-team#393 (comment) states:

We can implement Random for many more types, such as NonZero<T> and Wrapping<T>.

Tracking issue: #130703

Implement `Random` for `num::{NonZero,Saturating,Wrapping}`.
@rustbot
Copy link
Collaborator

rustbot commented Feb 8, 2025

r? @ibraheemdev

rustbot has assigned @ibraheemdev.
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 Feb 8, 2025
@sorairolake
Copy link
Contributor Author

r? @joboet

@rustbot rustbot assigned joboet and unassigned ibraheemdev Feb 8, 2025
Copy link
Member

@joboet joboet left a comment

Choose a reason for hiding this comment

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

The implementation looks correct, but this will need T-libs-api review as well.

r? libs-api

@rustbot rustbot added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Feb 8, 2025
@rustbot rustbot assigned BurntSushi and unassigned joboet Feb 8, 2025
@sorairolake
Copy link
Contributor Author

I think Saturating<T> and Wrapping<T> are "uniform" because all values ​​of T are valid. Unlike these, all values ​​of T ​​except zero are valid in NonZero<T>, so I think it is "almost uniform" rather than "uniform". So unless there is a way to generate an "uniform" NonZero<T>, it may be better not to add an implementation for this.

@sorairolake sorairolake changed the title Implement Random for num::{NonZero,Saturating,Wrapping} Implement Random for num::{Saturating,Wrapping} Feb 11, 2025
@ericlagergren
Copy link

I think Saturating<T> and Wrapping<T> are "uniform" because all values ​​of T are valid. Unlike these, all values ​​of T ​​except zero are valid in NonZero<T>, so I think it is "almost uniform" rather than "uniform". So unless there is a way to generate an "uniform" NonZero<T>, it may be better not to add an implementation for this.

RandomSource outputs uniformly random bytes, so NonZero<T> is uniformly random. It's just that its range is [1, N] instead of [0, N].

What would not be uniformly random is if (for example) NonZeroU32(42) were more likely than NonZeroU32(123). But that is not the case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants