// // Created by lk on 18-10-14. // #include <iostream> #include <cstdlib> #include <ctime> #include <vector> using namespace std; // 實例:產生一個長度為len的字符串驗證碼,並且由16個字母和0到9的數字組成 void RandStr(vector<char>&vec, const int len) { srand((int)time(0)); // 產生隨機種子 把0換成NULL也行 char temp; for (int i = 0; i < len; 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; } vec.push_back(temp); } } int main() { vector<char>vec; int len = 4; RandStr(vec, len); for (int i = 0; i < len; i++) { cout << vec[i]; } return 0; }
注意switch里面不能有臨時變量