C++ 隨機生成由大小寫字母和數字組成的字符串


頭文件

#include <time.h>  //秒

#include <sys/timeb.h> //毫秒

 1 std::string GetRandomStr(int len) {
 2         //毫秒
 3         struct timeb time_seed;
 4         ftime(&time_seed);
 5         srand(time_seed.time * 1000 + time_seed.millitm);
 6 
 7         // 8         //srand(time(0));
 9 
10         std::string random_str("");
11         for (int i = 0; i < len; ++i) {
12             switch (rand() % 3) {
13             case 1:
14                 random_str += ('A' + rand() % 26);
15                 break;
16             case 2:
17                 random_str += ('a' + rand() % 26);
18                 break;
19             default:
20                 random_str += ('0' + rand() % 10);
21                 break;
22             }
23         }
24 
25         return random_str;
26     }
27             

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM