本帖最后由 嘟嘟编程 于 2020-5-13 13:47 编辑
以下为随机数的定义方法,知道的可以跳过,表情符处是楼主的问题
代码如下:
//C语言
#include <stdio.h>
#include <time.h>//使用time函数,千万不要漏掉
#include <stdlib.h>//使用srand,rand函数,也千万不要漏了
int main()
{
int a;
srand ((unsigned)time(0));//unsignd类型:无符号,即不出现负数
a = rand();//前面的srand用来避免随机数重复,如果去掉,将每一次的随机数一样
//rand生成随机数
//此处输出省略
return 0;
}
//C++
#include <iostream>
#include <ctime>//使用time函数,千万不要漏掉
#include <cstdlib>//使用srand,rand函数,也千万不要漏了
using namespace std;
int main()
{
int a;
srand ((unsigned)time(0));//unsignd类型:无符号,即不出现负数
a = rand();//前面的srand用来避免随机数重复,如果去掉,将每一次的随机数一样
//rand生成随机数
//此处输出省略
return 0;
}
C/C++创建核心代码如下:
srand((unsigned)time(0));
a = rand();
若要生成n个随机数也只需要srand((unsigned)time(0));一次
即:
//这里以C语言为例
#include <stdio.h>
#include <time.h>//使用time函数,千万不要漏掉
#include <stdlib.h>//使用srand,rand函数,也千万不要漏了
int main()
{
int a, b;
srand ((unsigned)time(0));
a = rand();
b = rand();
return 0;
}
以此类推,三个也一样
希望能帮助到你
|