Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何制作;兰德();生成实际的随机数?_C_Random - Fatal编程技术网

C 如何制作;兰德();生成实际的随机数?

C 如何制作;兰德();生成实际的随机数?,c,random,C,Random,我得到了这个密码: #include <stdio.h> #include <conio.h> int rand(); int main() { int a = 1; while (a<=15) { printf("%d\n", rand()); a++; } return 0; } #包括 #包括 int ran

我得到了这个密码:

#include <stdio.h>
#include <conio.h>
int rand();

int main()
{
        int a = 1;

        while (a<=15)
        {
                printf("%d\n", rand());
                a++;
        }

        return 0;

}
#包括
#包括
int rand();
int main()
{
INTA=1;

而(a您需要使用
srand()
初始化您的
rand()
,如下所示:

#include <stdio.h>
#include <conio.h>
#include <time.h>
#include <stdlib.h> 

int main()
{
    srand(time(NULL));
    int a = 1;

    while (a<=15)
    {
            printf("%d\n", rand());
            a++;
    }

    return 0;
}
#包括
#包括
#包括
#包括
int main()
{
srand(时间(空));
INTA=1;

while(a您必须设置一个种子,所以只需在while循环之前执行此操作(也不要忘记包括:
time.h
):


您可以通过使用生成不同的随机数

#include <stdlib.h>   // for rand() and srand()
#include <time.h>     // for time()
// other headers

int main()
{
  srand(time(NULL));
  // rest of your code
}
#包括//for rand()和srand()
#包括//for time()
//其他标题
int main()
{
srand(时间(空));
//代码的其余部分
}
通过使用
srand()
,您可以为随机数生成器设定种子,以便在程序的不同运行中获得不同的随机数

并从代码中删除
int rand();
,除非您试图创建自己的
rand()
函数

如果u设置为时间,则包括

我建议您
包括
-这是用于srand或rand函数的

#include <stdlib.h>   // for rand() and srand()
#include <time.h>     // for time()
// other headers

int main()
{
  srand(time(NULL));
  // rest of your code
}