diff --git a/lab2/zad1/gen_asc/Cargo.lock b/lab2/zad1/gen_asc/Cargo.lock index da56c50..da99ecd 100644 --- a/lab2/zad1/gen_asc/Cargo.lock +++ b/lab2/zad1/gen_asc/Cargo.lock @@ -12,7 +12,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" name = "gen_asc" version = "0.1.0" dependencies = [ - "rand", + "libgen", ] [[package]] @@ -32,6 +32,13 @@ version = "0.2.153" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" +[[package]] +name = "libgen" +version = "0.1.0" +dependencies = [ + "rand", +] + [[package]] name = "ppv-lite86" version = "0.2.17" diff --git a/lab2/zad1/gen_asc/Cargo.toml b/lab2/zad1/gen_asc/Cargo.toml index aa353c7..950482e 100644 --- a/lab2/zad1/gen_asc/Cargo.toml +++ b/lab2/zad1/gen_asc/Cargo.toml @@ -4,4 +4,4 @@ version = "0.1.0" edition = "2021" [dependencies] -rand = "0.8.5" +libgen = { path = "../libgen" } diff --git a/lab2/zad1/gen_asc/src/main.rs b/lab2/zad1/gen_asc/src/main.rs index 34a39ea..62fdc88 100644 --- a/lab2/zad1/gen_asc/src/main.rs +++ b/lab2/zad1/gen_asc/src/main.rs @@ -1,6 +1,6 @@ use std::{env::args, io}; -use rand::Rng; +use libgen::gen_asc; fn main() -> io::Result<()> { @@ -11,13 +11,9 @@ fn main() -> io::Result<()> { println!("{}", amount); - let mut last = -1; - - for i in 1..=amount { - let current = rand::thread_rng().gen_range((last + 1)..=(2 * amount - (amount - i)) as i64); - println!("{}", current); - last = current; - } + gen_asc(amount) + .iter() + .for_each(|x| println!("{}", x)); Ok(()) } diff --git a/lab2/zad1/gen_desc/Cargo.lock b/lab2/zad1/gen_desc/Cargo.lock index e4c0c2f..c432d62 100644 --- a/lab2/zad1/gen_desc/Cargo.lock +++ b/lab2/zad1/gen_desc/Cargo.lock @@ -12,7 +12,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" name = "gen_desc" version = "0.1.0" dependencies = [ - "rand", + "libgen", ] [[package]] @@ -32,6 +32,13 @@ version = "0.2.153" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" +[[package]] +name = "libgen" +version = "0.1.0" +dependencies = [ + "rand", +] + [[package]] name = "ppv-lite86" version = "0.2.17" diff --git a/lab2/zad1/gen_desc/Cargo.toml b/lab2/zad1/gen_desc/Cargo.toml index d21d1a0..d46effa 100644 --- a/lab2/zad1/gen_desc/Cargo.toml +++ b/lab2/zad1/gen_desc/Cargo.toml @@ -4,4 +4,4 @@ version = "0.1.0" edition = "2021" [dependencies] -rand = "0.8.5" +libgen = { path = "../libgen" } diff --git a/lab2/zad1/gen_desc/src/main.rs b/lab2/zad1/gen_desc/src/main.rs index 40b987d..ee96448 100644 --- a/lab2/zad1/gen_desc/src/main.rs +++ b/lab2/zad1/gen_desc/src/main.rs @@ -1,6 +1,6 @@ use std::{env::args, io}; -use rand::Rng; +use libgen::gen_desc; fn main() -> io::Result<()> { @@ -11,14 +11,9 @@ fn main() -> io::Result<()> { println!("{}", amount); - let mut last = rand::thread_rng().gen_range((amount)..(2 * amount)); - println!("{}", last); - - for i in 1..amount { - let current = rand::thread_rng().gen_range((amount - i)..(last)); - println!("{}", current); - last = current; - } + gen_desc(amount) + .iter() + .for_each(|x| println!("{}", x)); Ok(()) } diff --git a/lab2/zad1/gen_rand/Cargo.lock b/lab2/zad1/gen_rand/Cargo.lock index 29d38de..02aac01 100644 --- a/lab2/zad1/gen_rand/Cargo.lock +++ b/lab2/zad1/gen_rand/Cargo.lock @@ -12,7 +12,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" name = "gen_rand" version = "0.1.0" dependencies = [ - "rand", + "libgen", ] [[package]] @@ -32,6 +32,13 @@ version = "0.2.153" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" +[[package]] +name = "libgen" +version = "0.1.0" +dependencies = [ + "rand", +] + [[package]] name = "ppv-lite86" version = "0.2.17" diff --git a/lab2/zad1/gen_rand/Cargo.toml b/lab2/zad1/gen_rand/Cargo.toml index 174aedd..8d566a2 100644 --- a/lab2/zad1/gen_rand/Cargo.toml +++ b/lab2/zad1/gen_rand/Cargo.toml @@ -4,4 +4,4 @@ version = "0.1.0" edition = "2021" [dependencies] -rand = "0.8.5" +libgen = { path = "../libgen" } diff --git a/lab2/zad1/gen_rand/src/main.rs b/lab2/zad1/gen_rand/src/main.rs index ad762b6..d36e463 100644 --- a/lab2/zad1/gen_rand/src/main.rs +++ b/lab2/zad1/gen_rand/src/main.rs @@ -1,6 +1,6 @@ use std::{env::args, io}; -use rand::Rng; +use libgen::gen_rand; fn main() -> io::Result<()> { @@ -11,9 +11,9 @@ fn main() -> io::Result<()> { println!("{}", amount); - for _ in 1..=amount { - println!("{}", rand::thread_rng().gen_range(0..(2 * amount))); - } + gen_rand(amount) + .iter() + .for_each(|x| println!("{}", x)); Ok(()) } diff --git a/lab2/zad1/libgen/Cargo.lock b/lab2/zad1/libgen/Cargo.lock new file mode 100644 index 0000000..1da0f1b --- /dev/null +++ b/lab2/zad1/libgen/Cargo.lock @@ -0,0 +1,75 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "getrandom" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94b22e06ecb0110981051723910cbf0b5f5e09a2062dd7663334ee79a9d1286c" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "libc" +version = "0.2.153" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" + +[[package]] +name = "libgen" +version = "0.1.0" +dependencies = [ + "rand", +] + +[[package]] +name = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" + +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" diff --git a/lab2/zad1/libgen/Cargo.toml b/lab2/zad1/libgen/Cargo.toml new file mode 100644 index 0000000..b2ef245 --- /dev/null +++ b/lab2/zad1/libgen/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "libgen" +version = "0.1.0" +edition = "2021" + +[dependencies] +rand = "0.8.5" diff --git a/lab2/zad1/libgen/src/lib.rs b/lab2/zad1/libgen/src/lib.rs new file mode 100644 index 0000000..cd3b7df --- /dev/null +++ b/lab2/zad1/libgen/src/lib.rs @@ -0,0 +1,36 @@ +use rand::Rng; + +pub fn gen_rand(amount: u64) -> Vec { + (1..=amount) + .map(|_| rand::thread_rng().gen_range(0..(2 * amount))) + .collect() +} + +pub fn gen_desc(amount: u64) -> Vec { + let mut ret = vec![]; + + let mut last = rand::thread_rng().gen_range((amount)..(2 * amount)); + ret.push(last); + + for i in 1..amount { + let current = rand::thread_rng().gen_range((amount - i)..(last)); + ret.push(current); + last = current; + } + + ret +} + +pub fn gen_asc(amount: u64) -> Vec { + let mut ret = vec![]; + + let mut last = -1; + + for i in 1..=amount { + let current = rand::thread_rng().gen_range((last + 1)..=(2 * amount - (amount - i)) as i64); + ret.push(current as u64); + last = current; + } + + ret +}