1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use core::ops::DerefMut;
use std::ops::Deref;
use rand::RngCore;
pub struct BufRand {
bit_buf: u64,
shift_counter: u8,
rand: Box<dyn RngCore>,
}
impl DerefMut for BufRand {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.rand
}
}
impl Deref for BufRand {
type Target = Box<dyn RngCore>;
fn deref(&self) -> &Self::Target {
&self.rand
}
}
impl BufRand {
pub fn new(rand: Box<dyn RngCore>) -> Self {
BufRand {
bit_buf: 0,
shift_counter: 0xff,
rand,
}
}
pub fn next_bool(&mut self) -> bool {
if self.shift_counter >= 64 {
self.bit_buf = self.next_u64();
self.shift_counter = 0;
}
let out = self.bit_buf % 2 == 0;
self.bit_buf >>= 1;
self.shift_counter += 1;
out
}
pub fn rand_char_case(&mut self, c: &char) -> Option<char> {
if self.next_bool() {
c.to_uppercase().to_string()
} else {
c.to_lowercase().to_string()
}
.chars()
.next()
}
pub fn rand_string_case(&mut self, s: &str) -> String {
s.chars()
.into_iter()
.map(|c| self.rand_char_case(&c).unwrap())
.collect()
}
}