@lowrey/

C++11 GUID Generation

C++11

No description

fork
loading
Files
  • main.cpp

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.9061395525365519","path":"main.cpp","file":{"path":"main.cpp","content":{"asEncoding":{"base64":"I2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3N0cmVhbT4KI2luY2x1ZGUgPHJhbmRvbT4KI2luY2x1ZGUgPHN0cmluZz4KCnVuc2lnbmVkIGNoYXIgcmFuZG9tX2NoYXIoKSB7CiAgICBzdGQ6OnJhbmRvbV9kZXZpY2UgcmQ7CiAgICBzdGQ6Om10MTk5MzcgZ2VuKHJkKCkpOwogICAgc3RkOjp1bmlmb3JtX2ludF9kaXN0cmlidXRpb248PiBkaXMoMCwgMjU1KTsKICAgIHJldHVybiBzdGF0aWNfY2FzdDx1bnNpZ25lZCBjaGFyPihkaXMoZ2VuKSk7Cn0KCnN0ZDo6c3RyaW5nIGdlbmVyYXRlX2hleChjb25zdCB1bnNpZ25lZCBpbnQgbGVuKSB7CiAgICBzdGQ6OnN0cmluZ3N0cmVhbSBzczsKICAgIGZvciAoYXV0byBpID0gMDsgaSA8IGxlbjsgaSsrKSB7CiAgICAgICAgY29uc3QgYXV0byByYyA9IHJhbmRvbV9jaGFyKCk7CiAgICAgICAgc3RkOjpzdHJpbmdzdHJlYW0gaGV4c3RyZWFtOwogICAgICAgIGhleHN0cmVhbSA8PCBzdGQ6OmhleCA8PCBpbnQocmMpOwogICAgICAgIGF1dG8gaGV4ID0gaGV4c3RyZWFtLnN0cigpOwogICAgICAgIHNzIDw8IChoZXgubGVuZ3RoKCkgPCAyID8gJzAnICsgaGV4IDogaGV4KTsKICAgIH0KICAgIHJldHVybiBzcy5zdHIoKTsKfQoKaW50IG1haW4oKQp7CiAgICBmb3IgKGF1dG8gaSA9IDA7IGkgPCA1OyBpKyspIHsKICAgICAgICBzdGQ6OmNvdXQgPDwgZ2VuZXJhdGVfaGV4KDYpIDw8IHN0ZDo6ZW5kbDsKICAgIH0KfQ=="},"asBuffer":null},"loaded":true}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <vector>
#include <iostream>
#include <sstream>
#include <random>
#include <string>

unsigned char random_char() {
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<> dis(0, 255);
    return static_cast<unsigned char>(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 << int(rc);
        auto hex = hexstream.str();
        ss << (hex.length() < 2 ? '0' + hex : hex);
    }
    return ss.str();
}

int main()
{
    for (auto i = 0; i < 5; i++) {
        std::cout << generate_hex(6) << std::endl;
    }
}