34 lines
757 B
C++
34 lines
757 B
C++
|
|
// https://mariusbancila.ro/blog/2018/02/27/stduuid-a-cpp-library-for-universally-unique-identifiers/
|
|
|
|
|
|
// #pragma once
|
|
|
|
#include <string>
|
|
#include <random>
|
|
#include <iostream>
|
|
|
|
std::string get_uuid() {
|
|
static std::random_device dev;
|
|
static std::mt19937_64 rng(dev());
|
|
|
|
std::uniform_int_distribution<int> dist(0, 15);
|
|
const char *v = "0123456789abcdef";
|
|
const bool dash[] = { 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0 };
|
|
|
|
std::string res;
|
|
res.reserve(40);
|
|
for (int i = 0; i < 16; i++) {
|
|
if (dash[i]) res += "-";
|
|
res += v[dist(rng)];
|
|
res += v[dist(rng)];
|
|
}
|
|
return res;
|
|
}
|
|
|
|
// g++ uuid_test.cpp
|
|
int main() {
|
|
auto uuid_str = get_uuid();
|
|
|
|
std::cout << uuid_str << std::endl;
|
|
} |