Skip to content

4.1 数组 randomAccess() #1909

@mazezen

Description

@mazezen

代码:

/* 随机访问元素 */
int randomAccess(int *nums, int size) {
// 在区间 [0, size) 中随机抽取一个数字
int randomIndex = rand() % size;
// 获取并返回随机元素
int randomNum = nums[randomIndex];
return randomNum;
}

问题描述:
使用rand() 之前没有加 srand()随机种子, 每次的随机数都是一样的,并没有实现随机效果

修改:
arc4random_uniform() 代替 rand()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions