Sinh chuỗi ngẫu nhiên độ dài n từ 26 chữ cái thường. Dùng rand() và mảng chứa alphabet.
Ví dụ
#include <bits/stdc++.h>
using namespace std;
const int MAX = 26;
string gen_random(int n) {
char alphabet[MAX] = {
'a','b','c','d','e','f','g',
'h','i','j','k','l','m','n',
'o','p','q','r','s','t','u',
'v','w','x','y','z'
};
string res = "";
for (int i = 0; i < n; i++)
res += alphabet[rand() % MAX];
return res;
}
int main() {
srand(time(NULL));
int n = 7;
cout << gen_random(n) << endl;
return 0;
}
Kết quả
gwqrgpa