We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 7400630 commit e3b2fbcCopy full SHA for e3b2fbc
rust/chapter_array_and_linkedlist/array.rs
@@ -4,10 +4,12 @@
4
* Author: xBLACICEx (xBLACKICEx@outlook.com), sjinzh (sjinzh@gmail.com)
5
*/
6
7
+use rand::Rng;
8
+
9
/* 随机返回一个数组元素 */
10
fn random_access(nums: &[i32]) -> i32 {
11
// 在区间 [0, nums.len()) 中随机抽取一个数字
- let random_index = rand::random::<usize>() % nums.len();
12
+ let random_index = rand::thread_rng().gen_range(0..nums.len());
13
// 获取并返回随机元素
14
let random_num = nums[random_index];
15
random_num
0 commit comments