feat: 470_implement_rand10_using_rand7
This commit is contained in:
parent
ba7ae5f04c
commit
480314c01e
7
470_implement_rand10_using_rand7/Cargo.lock
generated
Normal file
7
470_implement_rand10_using_rand7/Cargo.lock
generated
Normal file
@ -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"
|
6
470_implement_rand10_using_rand7/Cargo.toml
Normal file
6
470_implement_rand10_using_rand7/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "implement_rand10_using_rand7"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
24
470_implement_rand10_using_rand7/src/lib.rs
Normal file
24
470_implement_rand10_using_rand7/src/lib.rs
Normal file
@ -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
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user