Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I generate UUID in c++, without using boost library?

Tags:

c++

uuid

I want to generate UUID for my application to distinguish each installation of my application. I want to generate this UUID using C++ without boost library support. How can I generate UUID using some other opensource library?

Note: My platform is windows

like image 645
PURE Avatar asked Jun 23 '14 11:06

PURE


People also ask

How do you generate a random UUID in C++?

UUID::UUID uuid = uuidGenerator. getUUID(); std::string s = uuid. str();

What is UUID generator?

A UUID (Universal Unique Identifier) is a 128-bit value used to uniquely identify an object or entity on the internet. Depending on the specific mechanisms used, a UUID is either guaranteed to be different or is, at least, extremely likely to be different from any other UUID generated until A.D. 3400.

What is UUID string?

What is a UUID. Universally Unique Identifiers, or UUIDS, are 128 bit numbers, composed of 16 octets and represented as 32 base-16 characters, that can be used to identify information across a computer system. This specification was originally created by Microsoft and standardized by both the IETF and ITU.


1 Answers

This will do, if you're using modern C++.

#include <random> #include <sstream>  namespace uuid {     static std::random_device              rd;     static std::mt19937                    gen(rd());     static std::uniform_int_distribution<> dis(0, 15);     static std::uniform_int_distribution<> dis2(8, 11);      std::string generate_uuid_v4() {         std::stringstream ss;         int i;         ss << std::hex;         for (i = 0; i < 8; i++) {             ss << dis(gen);         }         ss << "-";         for (i = 0; i < 4; i++) {             ss << dis(gen);         }         ss << "-4";         for (i = 0; i < 3; i++) {             ss << dis(gen);         }         ss << "-";         ss << dis2(gen);         for (i = 0; i < 3; i++) {             ss << dis(gen);         }         ss << "-";         for (i = 0; i < 12; i++) {             ss << dis(gen);         };         return ss.str();     } } 
like image 193
happy_sisyphus Avatar answered Oct 10 '22 23:10

happy_sisyphus