rand()函数生成的随机数不是OS启动后每次都相同的吗,为什么下面可以得到10个不同的随机数?


#include <stdlib.h>  
#include <stdio.h>
 
// 回调函数
void populate_array(int *array, size_t arraySize, int (*getNextValue)(void))
{
    for (size_t i=0; i<arraySize; i++)
        array[i] = getNextValue();
}
 
// 获取随机值
int getNextRandomValue(void)
{
    return rand();
}
 
int main(void)
{
    int myarray[10];
    /* getNextRandomValue 不能加括号,否则无法编译,因为加上括号之后相当于传入此参数时传入了 int , 而不是函数指针*/
    populate_array(myarray, 10, getNextRandomValue);
    for(int i = 0; i < 10; i++) {
        printf("%d ", myarray[i]);
    }
    printf("\n");
    return 0;
}

输出:16807 282475249 1622650073 984943658 1144108930 470211272 101027544 1457850878 1458777923 2007237709

这里也没用srand()设置种子呀!

getNextValue是函数指针,是变量名,它指向的函数时getNextRandomValue.

getNextValue函数实现在哪呢
你放的代码里是getNextRandomValue,不是同一个函数