Skip to content

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.

@sorairolake
Copy link
Contributor Author

I'm closing this pull request because it's a hassle to support the new Random trait.

@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Dec 12, 2025
@sorairolake sorairolake deleted the add-random-for-num-module-types branch December 12, 2025 01:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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