C++利用随机数生成验证码
- #include <iostream>
- using namespace std;
- #include <cstdlib>
- #include <ctime>
- #include <string>
-
- void myrand(string &str,int leng)
- {
- srand(int(time(0)));//产生随机种子,保证每次的数据都不重复
- char temp= '0';
- for (int i = 0; i < leng; i++)
- {
- switch (rand() %% 3)
- {
- case 0:
- temp = rand() %% 10 + '0';
- break;
-
- case 1:
- temp = rand() %% 26 + 'a';
- break;
-
- case 2:
- temp = rand() %% 26 + 'A';
- break;
- }
- str += temp;
-
- }
-
-
- }
-
- int main()
- {
- string str;
- myrand(str,4);
- cout <<str <<endl;
- return 0;
- }
推荐阅读