2022-03-02 16:45:54 -08:00
|
|
|
use rand::{
|
|
|
|
|
distributions::Alphanumeric,
|
|
|
|
|
prelude::{Rng, SeedableRng, StdRng},
|
|
|
|
|
};
|
2019-01-25 12:05:21 -08:00
|
|
|
|
|
|
|
|
const OPERATORS: &[char] = &[
|
2022-11-14 16:04:37 -08:00
|
|
|
'+', '-', '<', '>', '(', ')', '*', '/', '&', '|', '!', ',', '.', '%',
|
2019-01-25 12:05:21 -08:00
|
|
|
];
|
|
|
|
|
|
2019-08-19 16:36:25 -07:00
|
|
|
pub struct Rand(StdRng);
|
2019-01-25 12:05:21 -08:00
|
|
|
|
|
|
|
|
impl Rand {
|
2024-09-07 20:13:58 -04:00
|
|
|
#[must_use]
|
2019-01-25 12:05:21 -08:00
|
|
|
pub fn new(seed: usize) -> Self {
|
2024-02-06 23:18:27 +01:00
|
|
|
Self(StdRng::seed_from_u64(seed as u64))
|
2019-01-25 12:05:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn unsigned(&mut self, max: usize) -> usize {
|
2024-04-09 21:42:59 -04:00
|
|
|
self.0.gen_range(0..=max)
|
2019-01-25 12:05:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn words(&mut self, max_count: usize) -> Vec<u8> {
|
|
|
|
|
let mut result = Vec::new();
|
|
|
|
|
let word_count = self.unsigned(max_count);
|
|
|
|
|
for i in 0..word_count {
|
|
|
|
|
if i > 0 {
|
|
|
|
|
if self.unsigned(5) == 0 {
|
2024-02-06 23:18:27 +01:00
|
|
|
result.push(b'\n');
|
2019-01-25 12:05:21 -08:00
|
|
|
} else {
|
2024-02-06 23:18:27 +01:00
|
|
|
result.push(b' ');
|
2019-01-25 12:05:21 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if self.unsigned(3) == 0 {
|
|
|
|
|
let index = self.unsigned(OPERATORS.len() - 1);
|
|
|
|
|
result.push(OPERATORS[index] as u8);
|
|
|
|
|
} else {
|
|
|
|
|
for _ in 0..self.unsigned(8) {
|
2024-02-06 23:18:27 +01:00
|
|
|
result.push(self.0.sample(Alphanumeric));
|
2019-01-25 12:05:21 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
result
|
|
|
|
|
}
|
|
|
|
|
}
|