随机产生字符串函数

用来产生一些测试数据用,用Dev C++和 VC都编译运行过了

Code:
  1. #include<stdio.h>   
  2. #include<iostream>   
  3. #include<time.h>   
  4. #include<stdlib.h>   
  5. #include<string>   
  6. using namespace std;   
  7.   
  8. string RandString(int n){ //参数n为字符串的最大长度    
  9.     int i,j,m;   
  10.     string s;   
  11.     char temp;   
  12.     j = rand() % 2;   
  13.     for(i = rand() % n; i < n; i++){   
  14.         if(j == 0) temp = 'a';   
  15.         else  temp = 'A';   
  16.         m = rand() % 26;   
  17.         temp = temp + m;   
  18.         s = s + temp;   
  19.     }   
  20.     return s;   
  21. }   
  22.   
  23. int main()   
  24. {   
  25.     int max,n;   
  26.     cout<<"请输入产生的字符串的最大长度:";   
  27.     cin>>max;   
  28.     cout<<endl<<"请输入要产生的字符串的个数:";   
  29.     cin>>n;    
  30.     string  s;   
  31.     srand((unsigned) time(NULL));   
  32.     for(int i = 0; i < n; i++){    
  33.         s = RandString(max);   
  34.         cout<<"随机产生的字符串为:"<<s<<endl;   
  35.     }   
  36.     system("pause");   
  37.     return 0;   
  38. }   
  39.        

 调用前要加 srand((unsigned) time(NULL));    产生种子!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值