-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathutils.rs
More file actions
34 lines (31 loc) · 1013 Bytes
/
utils.rs
File metadata and controls
34 lines (31 loc) · 1013 Bytes
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
use std::ops::{Add, Sub};
use rand::distributions::Uniform;
use rand::{thread_rng, Rng};
// random array that samples between min and max of T
pub fn get_random_array<T>(n: usize, min_value: T, max_value: T) -> Vec<T>
where
T: Copy + rand::distributions::uniform::SampleUniform,
{
let rng = thread_rng();
let uni = Uniform::new_inclusive(min_value, max_value);
rng.sample_iter(uni).take(n).collect()
}
// worst case array that alternates between increasing max and decreasing min values
pub fn get_worst_case_array<T>(n: usize, step: T) -> Vec<T>
where
T: Copy + Default + Sub<Output = T> + Add<Output = T>,
{
let mut arr: Vec<T> = Vec::with_capacity(n);
let mut min_value: T = Default::default();
let mut max_value: T = Default::default();
for i in 0..n {
if i % 2 == 0 {
arr.push(min_value);
min_value = min_value - step;
} else {
arr.push(max_value);
max_value = max_value + step;
}
}
arr
}