C++——生成UUID

#include <sstream>
#include <random>
#include <string>

unsigned int random_char() {
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<> dis(0, 255);
    return dis(gen);
}

std::string generate_hex(const unsigned int len) {
    std::stringstream ss;
    for (auto i = 0; i < len; i++) {
        const auto rc = random_char();
        std::stringstream hexstream;
        hexstream << std::hex << rc;
        auto hex = hexstream.str();
        ss << (hex.length() < 2 ? '0' + hex : hex);
    }
    return ss.str();
}

转自https://www.cnblogs.com/0523jy/p/11399578.html

posted @ 2023-07-18 21:46  摩天仑  阅读(669)  评论(0编辑  收藏  举报