diff --git a/470_implement_rand10_using_rand7/Cargo.lock b/470_implement_rand10_using_rand7/Cargo.lock new file mode 100644 index 0000000..a30a568 --- /dev/null +++ b/470_implement_rand10_using_rand7/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "implement_rand10_using_rand7" +version = "0.1.0" diff --git a/470_implement_rand10_using_rand7/Cargo.toml b/470_implement_rand10_using_rand7/Cargo.toml new file mode 100644 index 0000000..4ee6f15 --- /dev/null +++ b/470_implement_rand10_using_rand7/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "implement_rand10_using_rand7" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/470_implement_rand10_using_rand7/src/lib.rs b/470_implement_rand10_using_rand7/src/lib.rs new file mode 100644 index 0000000..f66de37 --- /dev/null +++ b/470_implement_rand10_using_rand7/src/lib.rs @@ -0,0 +1,24 @@ +#[inline] +fn rand7() -> i32 { + (std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_nanos() + % 7) as i32 +} + +pub struct Solution; + +impl Solution { + pub fn rand10() -> i32 { + let mut rnd: i32; + loop { + rnd = (rand7() - 1) * 7 + rand7(); // [1, 49] + + if rnd <= 40 { + break; + } + } + rnd % 10 + 1 + } +}