ofxVariableLab/src/random_id.h

30 lines
594 B
C
Raw Normal View History

2023-03-25 13:51:04 +01:00
#pragma once
#include <iostream>
#include <random>
namespace ofxVariableLab {
/// \brief create pseudo-random id
static std::string random_id(std::size_t len = 24){
static const std::string_view hex_chars = "0123456789abcdef";
using Generator = std::mt19937;
Generator gen{std::random_device{}()};
std::string uuid;
uuid.reserve(len);
while(uuid.size() < len){
auto n = gen();
for(auto i = Generator::max(); i & 0x8 && uuid.size() < len; i >>= 4){
uuid += hex_chars[n & 0xf];
n >>= 4;
}
}
return uuid;
}
}