随机数据教程

什么是随机数据:

emm…字面含义。

制作随机数据:

首先,你要用这个:

srand(time(0));//生成数据的由时间定,由于你运行的时间不同,你生成的数据也不会相同。

接着你就可以生成随机数据了。

如:

srand(time(0));
int a,b;

a=rand()%100,b=rand()%100;//将 a,b 赋予 0~99 之间的随机值

举个例子

A+B problem:

变化数据范围

数据范围: 1 ≤ a , b ≤ 1 0 9 1\le a,b \le 10^9 1a,b109

代码:

制造输入数据的:

#include<bits/stdc++.h>
using namespace std;
const int ab=1e9;//数据范围
int main()
{
	freopen("day1.in","w",stdout);//数据被传到了day1.in 里面了,你要先新建一个文本文件
	srand(time(0));
	cout<<rand()%ab+1<<" "<<rand()%ab+1<<endl;//将随机数据传入
	return 0;
} 

制造输出数据的:

#include<bits/stdc++.h>
using namespace std;
int main()
{
	freopen("day1.in","r",stdin);//提取 
	freopen("day1.out","w",stdout);//传入 
	int a,b;
	cin>>a>>b;
	cout<<a+b<<endl;
	return 0;
} 

数据范围: 0 ≤ a , b ≤ 1 0 9 0\le a,b \le 10^9 0a,b109

#include<bits/stdc++.h>
using namespace std;
const int ab=1e9;
int main()
{
	freopen("day1.in","w",stdout);
	srand(time(0));
	cout<<rand()%ab<<" "<<rand()%ab<<endl;//相较之前,只要改这一行
	return 0;
} 

A-B problem:

1 ≤ b ≤ a ≤ 1 0 9 1 \le b \le a \le 10^9 1ba109

#include<bits/stdc++.h>
using namespace std;
const int ab=1e9;
int main()
{
	freopen("day1.in","w",stdout);
	srand(time(0));
	int a,b; 
	a=rand()%ab+1,b=rand()%ab+1;
	while(a<b)
		a=rand()%ab+1,b=rand()%ab+1;//一直重新刷数据,直到 满足要求就会弹出 
	cout<<a<<" "<<b<<endl;
	return 0;
} 

类似的,你可以写出所有的随机数据代码(只要有标程)。

随机字符串(大写):
#include<bits/stdc++.h>
using namespace std;
const int l=1e3;//随机字符串的长度范围,这里以1e3为例 
int main()
{
	srand(time(0));
	int len=rand()%l;//随机长度
	for(int i=0;i<len;i++){
		char c=0;
		while(c<'A'||c>'Z'){
			int a=rand()%42+1;
			c=char(a+47);
		}
		cout<<c;
	}
	printf("\n");
	return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值