random_generator.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Boost random_generator.hpp header file ----------------------------------------------//
  2. // Copyright 2010 Andy Tompkins.
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_UUID_RANDOM_GENERATOR_HPP
  7. #define BOOST_UUID_RANDOM_GENERATOR_HPP
  8. #include <boost/uuid/uuid.hpp>
  9. #include <boost/uuid/seed_rng.hpp>
  10. #include <boost/random/uniform_int.hpp>
  11. #include <boost/random/variate_generator.hpp>
  12. #include <boost/random/mersenne_twister.hpp>
  13. #include <boost/assert.hpp>
  14. #include <boost/shared_ptr.hpp>
  15. #include <limits>
  16. namespace boost {
  17. namespace uuids {
  18. // generate a random-based uuid
  19. template <typename UniformRandomNumberGenerator>
  20. class basic_random_generator {
  21. private:
  22. typedef uniform_int<unsigned long> distribution_type;
  23. typedef variate_generator<UniformRandomNumberGenerator*, distribution_type> generator_type;
  24. struct null_deleter
  25. {
  26. void operator()(void const *) const {}
  27. };
  28. public:
  29. typedef uuid result_type;
  30. // default constructor creates the random number generator
  31. basic_random_generator()
  32. : pURNG(new UniformRandomNumberGenerator)
  33. , generator
  34. ( pURNG.get()
  35. , distribution_type
  36. ( (std::numeric_limits<unsigned long>::min)()
  37. , (std::numeric_limits<unsigned long>::max)()
  38. )
  39. )
  40. {
  41. // seed the random number generator
  42. detail::seed(*pURNG);
  43. }
  44. // keep a reference to a random number generator
  45. // don't seed a given random number generator
  46. explicit basic_random_generator(UniformRandomNumberGenerator& gen)
  47. : pURNG(&gen, null_deleter())
  48. , generator
  49. ( pURNG.get()
  50. , distribution_type
  51. ( (std::numeric_limits<unsigned long>::min)()
  52. , (std::numeric_limits<unsigned long>::max)()
  53. )
  54. )
  55. {}
  56. // keep a pointer to a random number generator
  57. // don't seed a given random number generator
  58. explicit basic_random_generator(UniformRandomNumberGenerator* pGen)
  59. : pURNG(pGen, null_deleter())
  60. , generator
  61. ( pURNG.get()
  62. , distribution_type
  63. ( (std::numeric_limits<unsigned long>::min)()
  64. , (std::numeric_limits<unsigned long>::max)()
  65. )
  66. )
  67. {
  68. BOOST_ASSERT(pURNG);
  69. }
  70. uuid operator()()
  71. {
  72. uuid u;
  73. int i=0;
  74. unsigned long random_value = generator();
  75. for (uuid::iterator it=u.begin(); it!=u.end(); ++it, ++i) {
  76. if (i==sizeof(unsigned long)) {
  77. random_value = generator();
  78. i = 0;
  79. }
  80. // static_cast gets rid of warnings of converting unsigned long to boost::uint8_t
  81. *it = static_cast<uuid::value_type>((random_value >> (i*8)) & 0xFF);
  82. }
  83. // set variant
  84. // must be 0b10xxxxxx
  85. *(u.begin()+8) &= 0xBF;
  86. *(u.begin()+8) |= 0x80;
  87. // set version
  88. // must be 0b0100xxxx
  89. *(u.begin()+6) &= 0x4F; //0b01001111
  90. *(u.begin()+6) |= 0x40; //0b01000000
  91. return u;
  92. }
  93. private:
  94. shared_ptr<UniformRandomNumberGenerator> pURNG;
  95. generator_type generator;
  96. };
  97. typedef basic_random_generator<mt19937> random_generator;
  98. }} // namespace boost::uuids
  99. #endif //BOOST_UUID_RANDOM_GENERATOR_HPP