Skip to content

Commit 84a373a

Browse files
committed
add randome get data
1 parent c9b13c3 commit 84a373a

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

Cargo.lock

+54
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ anyhow = "1.0.60"
2222
libm = "0.2.5"
2323
log = "0.4.17"
2424
num-traits = "0.2.15"
25+
rand = "0.8.5"
2526
sha2 = "0.10"
2627
thiserror = "1.0.61"
2728

rust/src/utils/mod.rs

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use rand::Rng;
2+
13
fn byte_to_hex(byte: &u8) -> String {
24
format!("{byte:02x}")
35
}
@@ -8,3 +10,12 @@ pub fn to_hex_string<T: Clone + Into<Vec<u8>>>(bytes: &T) -> String {
810

911
hex_vec.join("")
1012
}
13+
14+
/* 随机访问元素 */
15+
pub fn random_access<T: Clone>(nums: &[T]) -> T {
16+
// 在区间 [0, nums.len()) 中随机抽取一个数字
17+
let random_index = rand::thread_rng().gen_range(0..nums.len());
18+
// 获取并返回随机元素
19+
let random_num = &nums[random_index];
20+
random_num.clone()
21+
}

0 commit comments

Comments
 (0)