#1909·hello-algo

4.1 数组 randomAccess()

作者: mazezen创建于 2026年5月19日更新于 2026年5月19日

Code: /* Random access to elements */ int randomAccess(int *nums, int size) { // Randomly select a number in the range [0, size) int randomIndex = rand() % size; // Get and return the random element int randomNum = nums[randomIndex]; return randomNum; } Problem description: Without adding srand() random seed before rand(), the random numbers are always the same, and the random effect is not achieved. Modification: arc4random_uniform() replaces rand().

内容来源: krahets/hello-algo