随机化功能

Wor*_*ice 1 c arrays random

我正在尝试创建一个1000个元素数组.每个元素的范围应该在0到99之间.我首先尝试重现这个教学实例,实际上,我无法正确理解:

#include <stdio.h>
#include <stdlib.h>
#define DIM 1000

int myVect[DIM];
int i;
int main(){
randomize();                       /? Initialize randomize*/
    for (i =0; i < DIM ; i ++)
        myVect[i] = random(100);
return 0;
}
Run Code Online (Sandbox Code Playgroud)

在编译时,我得到了error,warningnote.

warning: implicit declaration of function ‘randomize’    (1)
error: too many arguments to function ‘random’           (2)
/usr/include/stdlib.h:282:17: note: declared here        (3)
Run Code Online (Sandbox Code Playgroud)

我从两个帖子中找到了资源:

不幸的是,由于我的经验不足,我无法理解我发布的教学实例的含义.

(1)在哪个图书馆randomize?我一直无法找到答案.

(2)Random不应该有参数.因此,为什么在它的例子中100,因为它应该确定随机数的范围?

(3)本说明是否表明random申报地点?

感谢您的耐心等待.

hac*_*cks 6

randomize()并且random()不是标准C库函数.你可以使用srandrand不是.

srand((unsigned)time(NULL));                       
for (i =0; i < DIM ; i ++)
    myVect[i] = rand()%100;
Run Code Online (Sandbox Code Playgroud)

对于time()功能,您需要包含<time.h>标题.