104

I want to generate just random UUID's, as it is just important for instances in my program to have unique identifiers. I looked into Boost UUID, but I can't manage to generate the UUID because I don't understand which class and method to use.

I would appreciate if someone could give me any example of how to achieve this.

danijar
  • 32,406
  • 45
  • 166
  • 297
Nikola
  • 1,406
  • 2
  • 16
  • 20

2 Answers2

189

A basic example:

#include <boost/uuid/uuid.hpp>            // uuid class
#include <boost/uuid/uuid_generators.hpp> // generators
#include <boost/uuid/uuid_io.hpp>         // streaming operators etc.

int main() {
    boost::uuids::uuid uuid = boost::uuids::random_generator()();
    std::cout << uuid << std::endl;
}

Example output:

7feb24af-fc38-44de-bc38-04defc3804de

Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
  • 6
    And how would you assign it to a string? Because I have a common base for every instance and I would need to concatenate UUID to a base. Thanks again! – Nikola Jul 15 '10 at 16:34
  • 24
    @nik: Use the [streaming support](http://www.boost.org/doc/libs/1_43_0/libs/uuid/uuid.html#boost/uuid/uuid_io.hpp) - there is a `stringstream` example. Or let `boost::lexical_cast(uuid)` do that for you. – Georg Fritzsche Jul 15 '10 at 16:41
  • 17
    As for the double parantheses: The first constructs an instance of `random_generator`, the second uses `operator()` on that instance. You should save the generator and call `operator()` on it if you want to generate more than one uuid: `random_generator rg; uuid ui = rg();` – Georg Fritzsche Jul 15 '10 at 16:44
  • 45
    @Nikola : use boost::uuids::to_string(uuid) for stringifying uuids – King Feb 28 '13 at 19:33
  • 1
    What is the generated UUID in the example based on? I want my UUID to include the network address and time. – danijar Aug 26 '13 at 07:49
  • 1
    @danijar: That's a [random one](http://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_.28random.29). Why do you need to include them? Can't you use/store those two values additionally if you really need them? – Georg Fritzsche Aug 28 '13 at 06:22
  • 2
    @GeorgFritzsche Together, exact time and machine's individual network address is uniquely. I though therefore it might be good key for the hash function. I don't need the clear values later on. But your idea has brought me an idea. It might be a good to use time and network address as seed for the random number generator or so. – danijar Aug 28 '13 at 08:19
  • 1
    @danijar: For practical purposes UUIDs can be considered unique - isn't that enough? Sounds like you really want to ask the question if the random generator that boost::uuids defaults to here is good enough for your usecase. I'd ask about that specifically and just use another one if it isn't. – Georg Fritzsche Aug 28 '13 at 09:29
43

The answer of Georg Fritzsche is ok but maybe a bit misleading. You should reuse the generator if you need more than one uuid. Maybe it's clearer this way:

#include <iostream>

#include <boost/uuid/uuid.hpp>            // uuid class
#include <boost/uuid/uuid_generators.hpp> // generators
#include <boost/uuid/uuid_io.hpp>         // streaming operators etc.


int main()
{
    boost::uuids::random_generator generator;

    boost::uuids::uuid uuid1 = generator();
    std::cout << uuid1 << std::endl;

    boost::uuids::uuid uuid2 = generator();
    std::cout << uuid2 << std::endl;

    return 0;
}
Nikko
  • 4,182
  • 1
  • 26
  • 44
  • 1
    Why should you re-use the generators? Is this a performance optimization or a safety tip? – Fred Sep 16 '15 at 00:03
  • 7
    It wouldn't be a very good universally unique ID if using a new generator caused uniqueness problems. – xaxxon Sep 16 '16 at 00:45
  • @xaxxon Do you have any documentation regarding the uniqueness problem when creating new generator. – Saneesh kumar Sep 27 '16 at 02:26
  • 2
    @Saneeshkumar It's a "universally unique identifier" not a "this generator unique identifier" for a reason. – xaxxon Sep 27 '16 at 02:46
  • 2
    Learn from me and make generators thread local. They're very expensive to seed – James Mar 07 '20 at 23:21
  • 2
    The official documentation only encourage to reuse a random_generator for performance sake, not safetiness. "Depending on the platform there may be a setup cost in initializing the generator so plan to reuse it if you can" _Source_: [Boost uuid 1.70.0](https://www.boost.org/doc/libs/1_70_0/libs/uuid/doc/uuid.html#Synopsis_random_generator) – Pablo Burgos Mar 30 '21 at 13:21