diff --git a/Core/Code/Algorithms/mitkUIDGenerator.cpp b/Core/Code/Algorithms/mitkUIDGenerator.cpp index 82f34e94ab..4ab5b7b477 100644 --- a/Core/Code/Algorithms/mitkUIDGenerator.cpp +++ b/Core/Code/Algorithms/mitkUIDGenerator.cpp @@ -1,122 +1,127 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include #include #include #include #include #include #include + #ifdef _MSC_VER #include "process.h" #else #include #endif namespace mitk { UIDGenerator::UIDGenerator(const char* prefix, unsigned int lengthOfRandomPart) :m_Prefix(prefix), - m_LengthOfRandomPart(lengthOfRandomPart) + m_LengthOfRandomPart(lengthOfRandomPart), + m_RandomGenerator(itk::Statistics::MersenneTwisterRandomVariateGenerator::New()) { if (lengthOfRandomPart < 5) { MITK_ERROR << "To few digits requested (" <Initialize(); + /* static int instanceID = 0; int processID = 0; #ifdef WIN32 processID = _getpid(); #else processID = getpid(); #endif unsigned int hash = seedhash( time(NULL), clock() ); unsigned int seed = (hash + processID) * 10 + instanceID; instanceID++; std::srand(seed); + */ } std::string UIDGenerator::GetUID() { std::ostringstream s; s << m_Prefix; time_t tt = time(0); tm* t = gmtime(&tt); if (t) { s << t->tm_year + 1900; if (t->tm_mon < 9) s << "0"; // add a 0 for months 1 to 9 s << t->tm_mon + 1; if (t->tm_mday < 10) s << "0"; // add a 0 for days 1 to 9 s << t->tm_mday; if (t->tm_hour < 10) s << "0"; // add a 0 for hours 1 to 9 s << t->tm_hour; if (t->tm_min < 10) s << "0"; // add a 0 for minutes 1 to 9 s << t->tm_min; if (t->tm_sec < 10) s << "0"; // add a 0 for seconds 1 to 9 s << t->tm_sec; std::ostringstream rs; - rs << (long int)( pow(10.0, double(m_LengthOfRandomPart)) / double(RAND_MAX) * double(rand()) ); + rs << (long int)( pow(10.0, double(m_LengthOfRandomPart)) / double(RAND_MAX) * double(m_RandomGenerator->GetUniformVariate(0, RAND_MAX)) ); for (size_t i = rs.str().length(); i < m_LengthOfRandomPart; ++i) { s << "X"; } s << rs.str(); } return s.str(); } unsigned int UIDGenerator::seedhash( time_t t, clock_t c ) { unsigned int h1 = 0; unsigned char *p = (unsigned char *) &t; for( size_t i = 0; i < sizeof(t); ++i ) { h1 *= 255 + 2U; h1 += p[i]; } unsigned int h2 = 0; p = (unsigned char *) &c; for( size_t j = 0; j < sizeof(c); ++j ) { h2 *= 255 + 2U; h2 += p[j]; } return h1 ^ h2; } } diff --git a/Core/Code/Algorithms/mitkUIDGenerator.h b/Core/Code/Algorithms/mitkUIDGenerator.h index 11b62b22c7..eb147c0f89 100644 --- a/Core/Code/Algorithms/mitkUIDGenerator.h +++ b/Core/Code/Algorithms/mitkUIDGenerator.h @@ -1,65 +1,67 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #ifndef MITK_UID_GENERATOR_INDCLUDED_FASAWE #define MITK_UID_GENERATOR_INDCLUDED_FASAWE #include #include #include +#include #ifdef _MSC_VER # pragma warning(push) # pragma warning(disable: 4251) #endif namespace mitk { /*! \brief Generated unique IDs Creates (somehow most of the time) unique IDs from a given prefix, the current date/time and a random part. The prefix is given to the constructor, together with the desired length of the random part (minimum 5 digits). The current implementation uses the time in milliseconds in combination with an internal counter and the process ID to initialze a std::rand random generator. This should at least assure a unique ID one the same machine. However, this method is limited and you might get problems if generating IDs on different machines. Still, there is also a very small chance to get identical random numbers. A feature request for improved UID generation is handled in BUG 14333. */ class MITK_CORE_EXPORT UIDGenerator { public: UIDGenerator(const char* prefix = "UID_", unsigned int lengthOfRandomPart = 8); /** @return Returns a unique ID as string. You will get another unique ID each time you call GetUID. */ std::string GetUID(); private: std::string m_Prefix; unsigned int m_LengthOfRandomPart; unsigned int seedhash( time_t t, clock_t c ); + itk::Statistics::MersenneTwisterRandomVariateGenerator::Pointer m_RandomGenerator; }; } #ifdef _MSC_VER # pragma warning(pop) #endif #endif