inline int setbits(const int a, const int b, const int mask) { return a | (b & mask); } static unsigned char random_char() { std::random_device rd; std::mt19937 gen(rd()); std::uniform_int_distribution<> dis(0, 255); return static_cast(dis(gen)); } static std::string generate_hex(const unsigned int len) { std::stringstream ss; for (auto i = 0; static_cast(i) < len; i++) { auto rc = random_char(); std::stringstream hexstream; hexstream << std::hex << int(rc); auto hex = hexstream.str(); ss << (hex.length() < 2 ? '0' + hex : hex); } return ss.str(); } std::string uuid() { // The UUID is of the form: xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx // // Where the 4 bits of digit M indicate the UUID version, and the 1–3 // most significant bits of digit N indicate the UUID variant. // // UUIDs generated here are version 4 variant 1. std::ostringstream ss; const int version = std::stoi(generate_hex(2), nullptr, 16); const int variant = std::stoi(generate_hex(2), nullptr, 16); ss << generate_hex(4) << "-" << generate_hex(2) << "-" << std::hex << setbits(0x4000, version, 0x0fff) << "-" << std::hex << setbits(0x8000, variant, 0x3fff) << "-" << generate_hex(6); return ss.str(); }