sts10 / phraze

Generate random passphrases
https://sts10.github.io/2023/10/24/phraze-passphrase-generator.html
Mozilla Public License 2.0
24 stars 3 forks source link

Add random number & symbol separators #3

Closed jc00ke closed 1 year ago

jc00ke commented 1 year ago

This adds special -s values that will generate random values.

jc00ke commented 1 year ago

I happened to see your post on Hachyderm and thought it would be fun on a Sunday to add random separators. This is my first Rust contribution, so if you have any feedback, I'm all ears!

I first went down the path of adding a new flag like -r but I thought it would end up being more changes and kinda weird. I'm not in love with the _* format, but thought it was a good place to start.

sts10 commented 1 year ago

ooo elegant. Thanks so much!

sts10 commented 1 year ago

One note for ya: This line Uniform::from(0..9).sample(rng).to_string() returns 0 through 9 exclusive, meaning it will never return 9, just 0 through 8.

You could make it inclusive with an =: Uniform::from(0..=9).sample(rng).to_string().

Though I'm going to use get_range() for now:

fn get_random_number(rng: &mut impl Rng) -> String {
    rng.gen_range(0..=9).to_string()
}

From the gen_range() docs:

This function is optimised for the case that only a single sample is made from the given range. See also the Uniform distribution type which may be faster if sampling from the same range repeatedly.

jc00ke commented 1 year ago

Oops, I didn't realize it was exclusive, good catch. gen_range looks good for this too.