Crates.io | adv_random |
lib.rs | adv_random |
version | 2.0.3 |
source | src |
created_at | 2023-05-05 05:39:32.764039 |
updated_at | 2024-10-20 06:45:09.189825 |
description | Create random numbers, passwords, or strings based on rules |
homepage | |
repository | https://github.com/mdavisJr/adv_random |
max_upload_size | |
id | 857309 |
size | 170,415 |
Create random numbers, passwords, or strings based on rules. You can choose your own random number generator to work with this framework. By default this framework will use rand if you do not provide a random number generator.
This framework comes with some standard rules that you can use. If you don't see a rule that you need, you can create your own custom rules to use with this framework or send me a request to add in a new rule.
Please see examples of using rules below. The framework can handle more combinations of rules than what is shown below. If you would like to see an example that isn't shown, send me a request and I'll add that example.
let random_result = random_numbers(&Settings::new(&[
Box::new(NumberRange::all(1, 20))
], 10));
match random_result.numbers() {
Ok(numbers) => {
println!("{:?}", numbers);
},
_ => println!("{:?}", random_result.logs())
}
let random_result = random_numbers(&Settings::new(&[
Box::new(NoDuplicate{}),
Box::new(NumberRange::all(1, 20))
], 10));
match random_result.numbers() {
Ok(numbers) => {
println!("{:?}", numbers);
},
_ => println!("{:?}", random_result.logs())
}
let random_result = random_numbers(&Settings::with_exclude_rules(&[
Box::new(NumberRange::all(1, 20))
], 10, Some(vec![Box::new(NoDuplicate{})])));
match random_result.numbers() {
Ok(numbers) => {
println!("{:?}", numbers);
},
_ => println!("{:?}", random_result.logs())
}
let random_result = random_numbers(&Settings::new(&[
Box::new(NumberRange::from_map(&[(&vec![0, 1], 100, 999), (&vec![2], 1000, 9999)]))
], 3));
match random_result.numbers() {
Ok(numbers) => {
println!("{:?}", numbers);
},
_ => println!("{:?}", random_result.logs())
}
let random_result = random_numbers(&Settings::with_exclude_rules(&[
Box::new(NoDuplicate{}),
Box::new(NumberRange::all(1, 100))
], 10, Some(vec![Box::new(NumberRange::all(20, 29)), Box::new(NumberRange::all(51, 75))])));
match random_result.numbers() {
Ok(numbers) => {
println!("{:?}", numbers);
},
_ => println!("{:?}", random_result.logs())
}
let random_result = random_numbers(&Settings::new(&[
Box::new(NoDuplicate{}),
Box::new(NumberPool::new(&[
NumberPoolItem::new("exclude_23", &PoolType::Set(HashSet::from_iter([23])), 0),
NumberPoolItem::new("must_contain_1_4", &PoolType::Set(HashSet::from_iter([1, 4])), 2),
NumberPoolItem::new("some_set", &PoolType::Set(HashSet::from_iter([17, 18, 19, 20, 21])), 3),
])),
Box::new(NumberRange::all(1, 100)),
], 10));
match random_result.numbers() {
Ok(numbers) => {
println!("{:?}", numbers);
},
_ => println!("{:?}", random_result.logs())
}
let random_result = random_numbers(&Settings::new(&[
Box::new(NoDuplicate{}),
Box::new(NumberRange::all(1, 10)),
Box::new(OddEvenByIndex::new(&vec![0, 2, 4], &vec![1,3])),
], 5));
match random_result.numbers() {
Ok(numbers) => {
println!("{:?}", numbers);
},
_ => println!("{:?}", random_result.logs())
}
let random_result = random_numbers(&Settings::new(&[
Box::new(NoDuplicate{}),
Box::new(NumberRange::all(1, 10)),
Box::new(OddEvenByIndex::new(&vec![3,4], &vec![0, 1, 2]))
], 5));
match random_result.numbers() {
Ok(numbers) => {
println!("{:?}", numbers);
},
_ => println!("{:?}", random_result.logs())
}
let random_result = random_numbers(&Settings::new(&[
Box::new(NoDuplicate{}),
Box::new(NumberRange::all(1, 10)),
Box::new(OddEvenByIndex::new(&vec![4], &vec![]))
], 5));
match random_result.numbers() {
Ok(numbers) => {
println!("{:?}", numbers);
},
_ => println!("{:?}", random_result.logs())
}
let random_result = random_numbers(&Settings::new(&[
Box::new(NoDuplicate{}),
Box::new(NumberRange::all(1, 20)),
Box::new(OddEven::new(5, 5))
], 10));
match random_result.numbers() {
Ok(numbers) => {
println!("{:?}", numbers);
},
_ => println!("{:?}", random_result.logs())
}
let random_result = random_numbers(&Settings::new(&[
Box::new(NumberRange::all(1, 20)),
Box::new(OddEven::new(0, 10))
], 10));
match random_result.numbers() {
Ok(numbers) => {
println!("{:?}", numbers);
},
_ => println!("{:?}", random_result.logs())
}
let random_result = random_numbers(&Settings::new(&[
Box::new(NoDuplicate{}),
Box::new(NumberRange::all(1, 10)),
Box::new(Sequential::new(5, &[]))
], 5));
match random_result.numbers() {
Ok(numbers) => {
println!("{:?}", numbers);
},
_ => println!("{:?}", random_result.logs())
}
let random_result = random_numbers(&Settings::new(&[
Box::new(NoDuplicate{}),
Box::new(NumberRange::all(1, 10)),
Box::new(Sequential::new(3, &[2]))
], 5));
match random_result.numbers() {
Ok(numbers) => {
println!("{:?}", numbers);
},
_ => println!("{:?}", random_result.logs())
}
let random_result = random_numbers(&Settings::new(&[
Box::new(NoDuplicate{}),
Box::new(NumberRange::all(1, 10)),
Box::new(Sequential::new(1, &[2, 2]))
], 5));
match random_result.numbers() {
Ok(numbers) => {
println!("{:?}", numbers);
},
_ => println!("{:?}", random_result.logs())
}
let random_result = random_numbers(&Settings::new(&[
Box::new(NumberPool::alphanumeric(20, false))
], 20));
match random_result.string(true) {
Ok(numbers) => {
println!("{:?}", numbers);
},
_ => println!("{:?}", random_result.logs())
}
let random_result = random_numbers(&Settings::new(&[
Box::new(NumberPool::alphanumeric(20, true))
], 20));
match random_result.string(true) {
Ok(numbers) => {
println!("{:?}", numbers);
},
_ => println!("{:?}", random_result.logs())
}
let random_result = random_numbers(&Settings::new(&[
Box::new(NoDuplicate{}),
Box::new(NumberPool::alphanumeric_specs(10, 10, 0))
], 20));
match random_result.string(true) {
Ok(numbers) => {
println!("{:?}", numbers);
},
_ => println!("{:?}", random_result.logs())
}
let random_result = random_numbers(&Settings::new(&[
Box::new(NoDuplicate{}),
Box::new(NumberPool::alphanumeric_specs(10, 10, 5))
], 25));
match random_result.string(true) {
Ok(numbers) => {
println!("{:?}", numbers);
},
_ => println!("{:?}", random_result.logs())
}
let random_result = random_numbers(&Settings::new(&[
Box::new(NumberPool::new(&[
NumberPoolItem::new("upper_case_alpha_set", &PoolType::new(&"ABCDEFGHIJKLMNOPQRSTUVWXYZ".chars().collect()), 11),
NumberPoolItem::new("numeric_set", &PoolType::new(&"365".chars().collect()), 2),
NumberPoolItem::new("special_char_set", &PoolType::new(&"#*".chars().collect()), 2),
]))
], 15));
match random_result.string(true) {
Ok(numbers) => {
println!("{:?}", numbers);
},
_ => println!("{:?}", random_result.logs())
}
let uc_alpha_set: HashSet<char> = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".chars().collect();
let random_result = random_numbers(&Settings::new(&[
Box::new(ExcludeNumberSets::new_string(&HashSet::from_iter(vec![String::from("AB1234567"), String::from("CB1234567")]))),
Box::new(NumberPoolByIndex::new(vec![
NumberPoolItemByIndex::new("upper_case_alpha_set", &PoolType::new(&uc_alpha_set.clone()), &HashSet::from_iter(vec![0, 1])),
NumberPoolItemByIndex::new("numeric_set", &PoolType::new(&"1234567890".chars().collect()), &HashSet::from_iter(vec![2, 3, 4, 5, 6, 7, 8])),
]))
], 9));
match random_result.string(false) {
Ok(numbers) => {
println!("{:?}", numbers);
},
_ => println!("{:?}", random_result.logs())
}
let random_result = random_numbers(&Settings::new(&[
Box::new(NumberRange::all(1, 100)),
Box::new(NumberSpace::new(&vec![NumberSpaceItem::new(&NumberSpaceType::Lt(3), 9)]))
], 10));
match random_result.numbers() {
Ok(numbers) => {
let mut sorted_numbers: Vec<usize> = numbers.to_vec();
sorted_numbers.sort_unstable();
println!("{:?}", sorted_numbers);
},
_ => println!("{:?}", random_result.logs())
}
let random_result = random_numbers(&Settings::new(&[
Box::new(NumberRange::all(1, 100)),
Box::new(NumberSpace::new(&vec![NumberSpaceItem::new(&NumberSpaceType::Lte(3), 9)]))
], 10));
match random_result.numbers() {
Ok(numbers) => {
let mut sorted_numbers: Vec<usize> = numbers.to_vec();
sorted_numbers.sort_unstable();
println!("{:?}", sorted_numbers);
},
_ => println!("{:?}", random_result.logs())
}
let random_result = random_numbers(&Settings::new(&[
Box::new(NumberRange::all(1, 100)),
Box::new(NumberSpace::new(&vec![NumberSpaceItem::new(&NumberSpaceType::Eq(3), 9)]))
], 10));
match random_result.numbers() {
Ok(numbers) => {
let mut sorted_numbers: Vec<usize> = numbers.to_vec();
sorted_numbers.sort_unstable();
println!("{:?}", sorted_numbers);
},
_ => println!("{:?}", random_result.logs())
}
let random_result = random_numbers(&Settings::new(&[
Box::new(NumberRange::all(1, 100)),
Box::new(NumberSpace::new(&vec![NumberSpaceItem::new(&NumberSpaceType::Gte(3), 9)]))
], 10));
match random_result.numbers() {
Ok(numbers) => {
let mut sorted_numbers: Vec<usize> = numbers.to_vec();
sorted_numbers.sort_unstable();
println!("{:?}", sorted_numbers);
},
_ => println!("{:?}", random_result.logs())
}
let random_result = random_numbers(&Settings::new(&[
Box::new(NumberRange::all(1, 100)),
Box::new(NumberSpace::new(&vec![NumberSpaceItem::new(&NumberSpaceType::Gt(3), 9)]))
], 10));
match random_result.numbers() {
Ok(numbers) => {
let mut sorted_numbers: Vec<usize> = numbers.to_vec();
sorted_numbers.sort_unstable();
println!("{:?}", sorted_numbers);
},
_ => println!("{:?}", random_result.logs())
}
let random_result = random_numbers(&Settings::new(&[
Box::new(NumberRange::all(1, 100)),
Box::new(NumberSpace::new(&vec![NumberSpaceItem::new(&NumberSpaceType::Between(3, 6), 9)]))
], 10));
match random_result.numbers() {
Ok(numbers) => {
println!("{:?}", numbers);
},
_ => println!("{:?}", random_result.logs())
}