Skip to content

Commit

Permalink
fix: no spaces and real random for letter challenge
Browse files Browse the repository at this point in the history
  • Loading branch information
SilenLoc committed Apr 29, 2023
1 parent 697cad3 commit e20c17e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
39 changes: 21 additions & 18 deletions src/random/letters.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
use rand::Rng;
use rand::prelude::Distribution;

pub fn random_letters_inner(max: u32) -> String {
let mut rng = rand::thread_rng();
let mut s: String = "".into();
for _i in 0..max {
let letter: char = rng.gen_range(b'A'..=b'Z') as char;
let manipulated = big_small_space(letter);
s.push(manipulated)
}
let rng = rand::thread_rng();
let s: String = rng
.sample_iter(&Alpha)
.take(max.try_into().unwrap_or_default())
.map(char::from)
.collect();
s.trim().to_owned()
}

fn big_small_space(letter: char) -> char {
let mut rng = rand::thread_rng();
let lr = rng.gen_range(0..3);
match lr {
0 => letter,
1 => {
let chars: Vec<char> = letter.to_lowercase().to_string().chars().collect();
chars[0]
struct Alpha;

impl Distribution<u8> for Alpha {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> u8 {
const RANGE: u32 = 26 + 26 + 1;
const GEN_ASCII_STR_CHARSET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ\
abcdefghijklmnopqrstuvwxyz\
";
loop {
let var = rng.next_u32() >> (32 - 5);
if var < RANGE {
return GEN_ASCII_STR_CHARSET[var as usize];
}
}
2 => ' ',
_ => letter,
}
}
}
2 changes: 1 addition & 1 deletion src/random/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub struct Algorithm {
pub const ALGS: [Algorithm; 4] = [
Algorithm {
id: "letters",
version: "0.1",
version: "0.2",
description: "some letters :)",
lang: "western",
out_size: &1,
Expand Down

0 comments on commit e20c17e

Please sign in to comment.