Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5975143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:02:30+00:00 2026-05-22T21:02:30+00:00

When I generate the random number with single thread, no duplicate in 4M uuids

  • 0

When I generate the random number with single thread, no duplicate in 4M uuids generated but if I generate with two threads each 1M, I see roughly 16-20 duplicates. What could be the reason?

class TestUuid 
{
 public:
  std::string GenerateUUid(){
       boost::uuids::uuid uid;
       {
          boost::mutex::scoped_lock(m_mRandomGen);
          uid = m_oRandomGen();
       }
       std::stringstream ss;
       ss << uid;
       return ss.str();
  }


  void TestUid(std::map<std::string, unsigned>& mUids, unsigned count){  
    for(unsigned i = 0; i < count; ++i) {
        std::string sUid = GenerateUUid();
        std::map<std::string, unsigned>::const_iterator it = mUids.find(sUid);           
        if(it == mUids.end()){
           mUids[sUid] = i;
        }else {
         std::cerr << "Duplicate uid:" << sUid << " found in  thread id:" << pthread_self() << ", counter:" << i << ", earlier counter:" << it->second << ", id:" << it->first<< std::endl;
        }
    } 
  }

   TestUnique() {
     unsigned count = 4000000;
     std::map<std::string, unsigned> uuids;
     TestUid(uuids, count);
   }

   TestUniqueMultiThread() {
    unsigned count = 1000000;
    std::map<std::string, unsigned> mUids1;
    boost::thread t1(boost::bind(&TestUuid::TestUid, this, mUids1, count));

    std::map<std::string, > Uunsignedids2;
    boost::thread t2(boost::bind(&TestUuid::TestUid, this,  mUids2, count));
    t1.join();
    t2.join();
   }

 private:
   boost::mutex m_mRandonGen;
   boost::uuids::random_generator m_oRandomGen;

}

int main() {
 TestUid oTest;
 oTest.TestUnique();  //work fine. no duplicate in 4M uuids
 oTest.TestUniqueMultiThread(); // around 16-20 duplicates in total 2*1M = 2M uuids
 return EXIT_SUCCESS;
}

below is the log.

Duplicate uid:9f4bfa5c-8e41-4012-ba3e-0b3e631834dc found in  thread id:1103669568, counter:12016, earlier counter:12015, id:9f4bfa5c-8e41-4012-ba3e-0b3e631834dc
Duplicate uid:0237b010-cb8f-4b89-9f47-042722902883 found in  thread id:1103669568, counter:65778, earlier counter:65777, id:0237b010-cb8f-4b89-9f47-042722902883
Duplicate uid:7a999ce7-0936-4642-b796-485334fc6ba4 found in  thread id:1093179712, counter:170570, earlier counter:170568, id:7a999ce7-0936-4642-b796-485334fc6ba4
Duplicate uid:09e1028b-5fc9-4fcd-ab70-991c02d47aec found in  thread id:1093179712, counter:208740, earlier counter:208739, id:09e1028b-5fc9-4fcd-ab70-991c02d47aec
Duplicate uid:66eb72f5-a3de-4941-8a64-6dad773f0ffb found in  thread id:1093179712, counter:211449, earlier counter:211448, id:66eb72f5-a3de-4941-8a64-6dad773f0ffb
Duplicate uid:8bccb459-1e70-4920-8486-6b0c5dcb3992 found in  thread id:1093179712, counter:212972, earlier counter:212971, id:8bccb459-1e70-4920-8486-6b0c5dcb3992
Duplicate uid:bb8109e3-6529-4122-a015-a9746900f692 found in  thread id:1093179712, counter:239296, earlier counter:239295, id:bb8109e3-6529-4122-a015-a9746900f692
Duplicate uid:a02ea282-b49b-4e4f-98a3-01406824c888 found in  thread id:1103669568, counter:338582, earlier counter:338581, id:a02ea282-b49b-4e4f-98a3-01406824c888
Duplicate uid:8bc848d7-bbe9-405c-9ef3-4d5ec312aa5e found in  thread id:1093179712, counter:472035, earlier counter:472010, id:8bc848d7-bbe9-405c-9ef3-4d5ec312aa5e
Duplicate uid:d3d8e09f-c410-4ce0-9a75-2a0c363db89c found in  thread id:1093179712, counter:531441, earlier counter:531440, id:d3d8e09f-c410-4ce0-9a75-2a0c363db89c
Duplicate uid:3130184f-345e-4d1c-bb01-d481eec29704 found in  thread id:1093179712, counter:548770, earlier counter:548769, id:3130184f-345e-4d1c-bb01-d481eec29704
Duplicate uid:29572641-2487-400a-926f-9bbf7ca176b4 found in  thread id:1093179712, counter:710813, earlier counter:710811, id:29572641-2487-400a-926f-9bbf7ca176b4
Duplicate uid:36b3567d-5f06-4c72-a395-e6f6ce056c6b found in  thread id:1093179712, counter:728598, earlier counter:728597, id:36b3567d-5f06-4c72-a395-e6f6ce056c6b
Duplicate uid:3290cb7e-2535-43bc-b53c-71ac0bc4fca1 found in  thread id:1103669568, counter:846883, earlier counter:846881, id:3290cb7e-2535-43bc-b53c-71ac0bc4fca1
Duplicate uid:59137657-2b2a-473e-b12c-1890d6058ca2 found in  thread id:1093179712, counter:814812, earlier counter:814810, id:59137657-2b2a-473e-b12c-1890d6058ca2

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T21:02:31+00:00Added an answer on May 22, 2026 at 9:02 pm

    This is a common error when using RAII locks: you forgot to give your lock a name in the line

          boost::mutex::scoped_lock(m_mRandomGen);
    

    so it didn’t lock anything at all. Change it to

          boost::mutex::scoped_lock lk(m_mRandonGen); // note the typo in mutex name
    

    EDIT: what really happened: There was no compiler error despite the typo in the mutex name because the declaration

    type(name);
    

    is the same as

    type name;
    

    if the name has not been declared before. In other words, you’ve default-constructed a new scoped_lock called m_mRandomGen, not associated with a mutex.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know how to generate a single random number within a given range, a
i am trying to generate random number for my mental math quiz game. But
I need to be able to generate a single random number and then use
Possible Duplicate: Javascript/html: How to generate random number between number A and number B?
I want to generate a random number with in two range, and i want
how to generate random number in this code? I try use $RANDOM, but the
i can generate random number between two numbers in c using this.. arc4random()%(high-low+1)+low; then
How to generate random number from an array? and not from a range. int
Is there a way to generate random number on Windows by reading from a
How do I generate a random number between 0 and n ?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.