scirs2_core/random/
quick.rs1use crate::random::{random_normal_array, random_uniform_array, thread_rng};
27use ::ndarray::{Array2, Ix2};
28use rand_distr::{Normal, Uniform};
29
30pub fn random_f64() -> f64 {
32 crate::random::convenience::uniform()
33}
34
35pub fn random_f32() -> f32 {
37 random_f64() as f32
38}
39
40pub fn random_int(min: i64, max: i64) -> i64 {
42 crate::random::convenience::integer(min, max)
43}
44
45pub fn random_usize(min: usize, max: usize) -> usize {
47 random_int(min as i64, max as i64) as usize
48}
49
50pub fn random_bool() -> bool {
52 crate::random::convenience::boolean()
53}
54
55pub fn random_bool_with_prob(prob: f64) -> bool {
57 random_f64() < prob
58}
59
60pub fn random_vector(size: usize) -> Vec<f64> {
62 let mut rng = thread_rng();
63 (0..size)
64 .map(|_| rng.sample(Uniform::new(0.0, 1.0).expect("Operation failed")))
65 .collect()
66}
67
68pub fn random_int_vector(size: usize, min: i64, max: i64) -> Vec<i64> {
70 (0..size).map(|_| random_int(min, max)).collect()
71}
72
73pub fn random_matrix(rows: usize, cols: usize) -> Array2<f64> {
75 let mut rng = thread_rng();
76 random_uniform_array(Ix2(rows, cols), &mut rng)
77}
78
79pub fn random_normal_matrix(rows: usize, cols: usize, mean: f64, std: f64) -> Array2<f64> {
81 let mut rng = thread_rng();
82 random_normal_array(Ix2(rows, cols), mean, std, &mut rng)
83}
84
85pub fn pick_one<T>(items: &[T]) -> Option<&T> {
87 if items.is_empty() {
88 None
89 } else {
90 let index = random_usize(0, items.len() - 1);
91 Some(&items[index])
92 }
93}
94
95pub fn pick_many<T: Clone>(items: &[T], count: usize) -> Vec<T> {
97 (0..count)
98 .filter_map(|_| pick_one(items))
99 .cloned()
100 .collect()
101}
102
103pub fn shuffle<T>(items: &mut Vec<T>) {
105 use crate::random::seq::SliceRandom;
106 let mut rng = crate::random::thread_rng();
107 items.shuffle(&mut rng);
108}
109
110pub fn shuffled<T: Clone>(items: &[T]) -> Vec<T> {
112 let mut result = items.to_vec();
113 shuffle(&mut result);
114 result
115}
116
117pub fn random_text(length: usize) -> String {
119 const CHARS: &[u8] = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
120 (0..length)
121 .map(|_| {
122 let idx = random_usize(0, CHARS.len() - 1);
123 CHARS[idx] as char
124 })
125 .collect()
126}
127
128pub fn random_hex(byte_length: usize) -> String {
130 (0..byte_length)
131 .map(|_| format!("{:02x}", random_usize(0, 255)))
132 .collect()
133}
134
135pub fn coin_flip() -> bool {
137 random_bool()
138}
139
140pub fn dice_roll() -> i64 {
142 random_int(1, 6)
143}
144
145pub fn dice_roll_sum(count: usize) -> i64 {
147 (0..count).map(|_| dice_roll()).sum()
148}
149
150pub fn random_percentage() -> f64 {
152 random_f64() * 100.0
153}
154
155pub fn weighted_choice<T: Clone>(items: &[T], weights: &[f64]) -> Option<T> {
157 if items.len() != weights.len() || items.is_empty() {
158 return None;
159 }
160
161 let total: f64 = weights.iter().sum();
162 if total <= 0.0 {
163 return None;
164 }
165
166 let mut cumulative = 0.0;
167 let target = random_f64() * total;
168
169 for (item, &weight) in items.iter().zip(weights.iter()) {
170 cumulative += weight;
171 if target <= cumulative {
172 return Some(item.clone());
173 }
174 }
175
176 items.last().cloned()
178}