4.1 数组 randomAccess()
Author: mazezenCreated May 19, 2026Updated May 19, 2026
代码:
/* 随机访问元素 */ int randomAccess(int *nums, int size) { // 在区间 [0, size) 中随机抽取一个数字 int randomIndex = rand() % size; // 获取并返回随机元素 int randomNum = nums[randomIndex]; return randomNum; }
问题描述: 使用rand() 之前没有加 srand()随机种子, 每次的随机数都是一样的,并没有实现随机效果
修改: arc4random_uniform() 代替 rand()
Source: krahets/hello-algo