Skip to content

Commit e3b2fbc

Browse files
committed
update
1 parent 7400630 commit e3b2fbc

File tree

1 file changed

+3
-1
lines changed
  • rust/chapter_array_and_linkedlist

1 file changed

+3
-1
lines changed

rust/chapter_array_and_linkedlist/array.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
* Author: xBLACICEx (xBLACKICEx@outlook.com), sjinzh (sjinzh@gmail.com)
55
*/
66

7+
use rand::Rng;
8+
79
/* 随机返回一个数组元素 */
810
fn random_access(nums: &[i32]) -> i32 {
911
// 在区间 [0, nums.len()) 中随机抽取一个数字
10-
let random_index = rand::random::<usize>() % nums.len();
12+
let random_index = rand::thread_rng().gen_range(0..nums.len());
1113
// 获取并返回随机元素
1214
let random_num = nums[random_index];
1315
random_num

0 commit comments

Comments
 (0)