用来产生一些测试数据用,用Dev C++和 VC都编译运行过了
- #include<stdio.h>
- #include<iostream>
- #include<time.h>
- #include<stdlib.h>
- #include<string>
- using namespace std;
- string RandString(int n){ //参数n为字符串的最大长度
- int i,j,m;
- string s;
- char temp;
- j = rand() % 2;
- for(i = rand() % n; i < n; i++){
- if(j == 0) temp = 'a';
- else temp = 'A';
- m = rand() % 26;
- temp = temp + m;
- s = s + temp;
- }
- return s;
- }
- int main()
- {
- int max,n;
- cout<<"请输入产生的字符串的最大长度:";
- cin>>max;
- cout<<endl<<"请输入要产生的字符串的个数:";
- cin>>n;
- string s;
- srand((unsigned) time(NULL));
- for(int i = 0; i < n; i++){
- s = RandString(max);
- cout<<"随机产生的字符串为:"<<s<<endl;
- }
- system("pause");
- return 0;
- }
调用前要加 srand((unsigned) time(NULL)); 产生种子!