相关疑难解决方法(0)

如何在C++中生成随机字符串?

我正在寻找在C++中生成随机字符串的方法.这是我的代码:

string randomStrGen(int length) {
    static string charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
    string result;
    result.resize(length);

    srand(time(NULL));
    for (int i = 0; i < length; i++)
        result[i] = charset[rand() % charset.length()];

    return result;
}
Run Code Online (Sandbox Code Playgroud)

但这seed(time(NULL))不是随机的.还有其他更好的方法在C++中生成随机字符串吗?

c++ string random

12
推荐指数
1
解决办法
2万
查看次数

标签 统计

c++ ×1

random ×1

string ×1