1 // Boost random_generator.hpp header file ----------------------------------------------//
3 // Copyright 2010 Andy Tompkins.
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
8 #ifndef BOOST_UUID_RANDOM_GENERATOR_HPP
9 #define BOOST_UUID_RANDOM_GENERATOR_HPP
11 #include <boost/uuid/uuid.hpp>
12 #include <boost/uuid/seed_rng.hpp>
13 #include <boost/random/uniform_int.hpp>
14 #include <boost/random/variate_generator.hpp>
15 #include <boost/random/mersenne_twister.hpp>
16 #include <boost/assert.hpp>
17 #include <boost/shared_ptr.hpp>
23 // generate a random-based uuid
24 template <typename UniformRandomNumberGenerator>
25 class basic_random_generator {
27 typedef uniform_int<unsigned long> distribution_type;
28 typedef variate_generator<UniformRandomNumberGenerator*, distribution_type> generator_type;
32 void operator()(void const *) const {}
36 typedef uuid result_type;
38 // default constructor creates the random number generator
39 basic_random_generator()
40 : pURNG(new UniformRandomNumberGenerator)
44 ( (std::numeric_limits<unsigned long>::min)()
45 , (std::numeric_limits<unsigned long>::max)()
49 // seed the random number generator
53 // keep a reference to a random number generator
54 // don't seed a given random number generator
55 explicit basic_random_generator(UniformRandomNumberGenerator& gen)
56 : pURNG(&gen, null_deleter())
60 ( (std::numeric_limits<unsigned long>::min)()
61 , (std::numeric_limits<unsigned long>::max)()
66 // keep a pointer to a random number generator
67 // don't seed a given random number generator
68 explicit basic_random_generator(UniformRandomNumberGenerator* pGen)
69 : pURNG(pGen, null_deleter())
73 ( (std::numeric_limits<unsigned long>::min)()
74 , (std::numeric_limits<unsigned long>::max)()
86 unsigned long random_value = generator();
87 for (uuid::iterator it=u.begin(); it!=u.end(); ++it, ++i) {
88 if (i==sizeof(unsigned long)) {
89 random_value = generator();
93 // static_cast gets rid of warnings of converting unsigned long to boost::uint8_t
94 *it = static_cast<uuid::value_type>((random_value >> (i*8)) & 0xFF);
99 *(u.begin()+8) &= 0xBF;
100 *(u.begin()+8) |= 0x80;
103 // must be 0b0100xxxx
104 *(u.begin()+6) &= 0x4F; //0b01001111
105 *(u.begin()+6) |= 0x40; //0b01000000
111 shared_ptr<UniformRandomNumberGenerator> pURNG;
112 generator_type generator;
115 typedef basic_random_generator<mt19937> random_generator;
117 }} // namespace boost::uuids
119 #endif //BOOST_UUID_RANDOM_GENERATOR_HPP