一、生成随机数 (测试用,考试文件别用)
[cstdlib] rand()
rand()不需要参数,它会返回一个从0到RAND_MAX(最小为32767)的任意整数。
如果你要产生0~10的10个整数,可以表达为:
int N = rand() % 11;
如果要产生1~10
int N = 1 + rand() % 10;
总结来说,可以表示为: a + rand() % (b-a+1) (a是起始值,b是终止值)
生成0-0.9的小数
生成0-0.001 的小数
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
int main() {
//freopen("~~题目名称~~.in","r",stdin);
//freopen("test2.in","w",stdout);
srand(time(0));
for(int i=0;i<50;i++){
cout<<1 + rand() % 100<<" ";
}
fclose(stdin);fclose(stdout);
return 0;
}
- srand(time(0));
[ctime] time(0) clock()
- cout<<time(0); //返回从 1970-01-01 00:00:00 至今的秒数
参数解释(选学):
time_t seconds;
time(&seconds); //1970-01-01 00:00:00 至今的秒数
- cout<<clock()<<endl;从执行程序起的毫秒数
二、对拍
cmd 打开命令提示符窗口
f: 回车
fc 1.out 2.out
若多次生成随机in文件,多次判断两个程序结果是否想相同,写成 批处理.bat
@echo off
:loop
rand.exe
my.exe
std.exe
fc 1.out 2.out
if not errorlevel 1 goto loop
pause
goto loop
其中 1.echo off 是关掉命令显示 2. loop 名字自定义 3.errorlevel 0代表一样 1代表不一样