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

Alphanumeric samples bytes instead of chars #1012

Merged
merged 3 commits into from Aug 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -19,6 +19,7 @@ You may also find the [Upgrade Guide](https://rust-random.github.io/book/update.
is supported (#744, #1003). Note that `a` and `b` can no longer be references or SIMD types.
- Replace `AsByteSliceMut` with `Fill` (#940)
- Move alias method for `WeightedIndex` to `rand_distr` (#945)
- `Alphanumeric` samples bytes instead of chars (#935)
- Better NaN handling for `WeightedIndex` (#1005)
- Implement `IntoIterator` for `IndexVec`, replacing the `into_iter` method (#1007)
- Reduce packaged crate size (#983)
Expand Down
2 changes: 1 addition & 1 deletion benches/distributions.rs
Expand Up @@ -176,7 +176,7 @@ distr_nz_int!(distr_standard_nz64, NonZeroU64, u64, Standard);
distr_nz_int!(distr_standard_nz128, NonZeroU128, u128, Standard);

distr!(distr_standard_bool, bool, Standard);
distr!(distr_standard_alphanumeric, char, Alphanumeric);
distr!(distr_standard_alphanumeric, u8, Alphanumeric);
distr!(distr_standard_codepoint, char, Standard);

distr_float!(distr_standard_f32, f32, Standard);
Expand Down
2 changes: 1 addition & 1 deletion src/distributions/mod.rs
Expand Up @@ -166,7 +166,7 @@ pub trait Distribution<T> {
/// let v: Vec<f32> = Standard.sample_iter(rng).take(16).collect();
///
/// // String:
/// let s: String = Alphanumeric.sample_iter(rng).take(7).collect();
/// let s: String = Alphanumeric.sample_iter(rng).take(7).map(char::from).collect();
///
/// // Dice-rolling:
/// let die_range = Uniform::new_inclusive(1, 6);
Expand Down
13 changes: 7 additions & 6 deletions src/distributions/other.rs
Expand Up @@ -19,7 +19,7 @@ use serde::{Serialize, Deserialize};

// ----- Sampling distributions -----

/// Sample a `char`, uniformly distributed over ASCII letters and numbers:
/// Sample a `u8`, uniformly distributed over ASCII letters and numbers:
/// a-z, A-Z and 0-9.
///
/// # Example
Expand All @@ -32,6 +32,7 @@ use serde::{Serialize, Deserialize};
/// let mut rng = thread_rng();
/// let chars: String = iter::repeat(())
/// .map(|()| rng.sample(Alphanumeric))
/// .map(char::from)
/// .take(7)
/// .collect();
/// println!("Random chars: {}", chars);
Expand Down Expand Up @@ -64,8 +65,8 @@ impl Distribution<char> for Standard {
}
}

impl Distribution<char> for Alphanumeric {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> char {
impl Distribution<u8> for Alphanumeric {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> u8 {
const RANGE: u32 = 26 + 26 + 10;
const GEN_ASCII_STR_CHARSET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ\
abcdefghijklmnopqrstuvwxyz\
Expand All @@ -77,7 +78,7 @@ impl Distribution<char> for Alphanumeric {
loop {
let var = rng.next_u32() >> (32 - 6);
if var < RANGE {
return GEN_ASCII_STR_CHARSET[var as usize] as char;
return GEN_ASCII_STR_CHARSET[var as usize];
}
}
}
Expand Down Expand Up @@ -221,7 +222,7 @@ mod tests {
// take the rejection sampling path.
let mut incorrect = false;
for _ in 0..100 {
let c = rng.sample(Alphanumeric);
let c: char = rng.sample(Alphanumeric).into();
incorrect |= !((c >= '0' && c <= '9') ||
(c >= 'A' && c <= 'Z') ||
(c >= 'a' && c <= 'z') );
Expand Down Expand Up @@ -249,7 +250,7 @@ mod tests {
'\u{ed692}',
'\u{35888}',
]);
test_samples(&Alphanumeric, 'a', &['h', 'm', 'e', '3', 'M']);
test_samples(&Alphanumeric, 0, &[104, 109, 101, 51, 77]);
Copy link
Member

Choose a reason for hiding this comment

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

Now we need .map(..) for distributions?

Possible I think, and in a way it makes sense. What do you think?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Now we need .map(..) for distributions?

How do you mean that? You need that if you want char, but I think that makes sense, because the conversion from u8 to char is trivial, but the other direction is not. I think it makes more sense to see Alphanumeric as a distribution of bytes, because this type is more narrow, and it's unfortunate to throw away that compile-time knowledge by forcing a conversion to char.

If you prefer, we can also use this for the test:

Suggested change
test_samples(&Alphanumeric, 0, &[104, 109, 101, 51, 77]);
test_samples(&Alphanumeric, b'a', &[b'h', b'm', b'e', b'3', b'M']);

Copy link
Member

@dhardy dhardy Aug 2, 2020

Choose a reason for hiding this comment

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

(Edit: start again.)

The point is that here you could replace &Alphanumeric with &Alphanumeric.map(char::from) and keep the other args to test_samples as chars. Of course that doesn't matter for this test, but may be mildly useful elsewhere — though maybe not often, since we can already do Alphanumeric.sample_iter(rng).map(char::from).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

At this point, we might as well implement Iterator for distributions, no?

Copy link
Member

Choose a reason for hiding this comment

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

We do: the .sample_iter(rng) method. The RNG has to be attached somehow.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think it is preferable to convert the distribution into an iterator for such cases. This also supports all the other Iterator methods, without adding more API.

test_samples(&Standard, false, &[true, true, false, true, false]);
test_samples(&Standard, None as Option<bool>, &[
Some(true),
Expand Down
2 changes: 1 addition & 1 deletion src/rng.rs
Expand Up @@ -171,7 +171,7 @@ pub trait Rng: RngCore {
/// let v: Vec<f32> = rng.sample_iter(Standard).take(16).collect();
///
/// // String:
/// let s: String = rng.sample_iter(Alphanumeric).take(7).collect();
/// let s: String = rng.sample_iter(Alphanumeric).take(7).map(char::from).collect();
///
/// // Combined values
/// println!("{:?}", rng.sample_iter(Standard).take(5)
Expand Down