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 6850523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:08:06+00:00 2026-05-27T01:08:06+00:00

I recently learned the hard way that #<cstdlib> rand() is not thread safe, and

  • 0

I recently learned the hard way that #<cstdlib> rand() is not thread safe, and on Linux is implemented with mutexes, causing a bottleneck when rand() is called frequently by multiple threads. rand_r works as a replacement, but there are concerns about the quality of random number generation. Moreover, this situation has caused me to question whether there might be faster random number generators out there, since apparently my code spends a lot of time generating random numbers. There are a few alternatives listed in the link above, but I’m unsure as to their speed and to what other alternatives might be out there.

  • 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-27T01:08:06+00:00Added an answer on May 27, 2026 at 1:08 am

    If you don’t need any statistical control across threads, just use the facilities provided by <random>:

    #include <random>
    
    typedef std:::mt19937 rng_type;
    std::uniform_int_distribution<rng_type::result_type> udist(0, 200);
    
    int main()  // this can be per thread!
    {
      rng_type rng;
    
      // seed rng first:
      rng_type::result_type const seedval = get_seed();
      rng.seed(seedval);
    
      rng_type::result_type random_number = udist(rng);
    
      return random_number;
    }
    

    The Mersenne twister PRNG is both fast and has good statistical properties. Maintaining a separate (and separately seeded) engine object in each thread you avoid all concurrency issues.

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

Sidebar

Related Questions

I've recently learned that on linux, git can be setup to autocomplete commands: If
I recently learned that Unicode is permitted within Java source code not only as
I recently learned that all stl containers have swap function: i.e. c1.swap(c2); will lead
I recently learned that I can get hg log to print the history in
I recently learned how to use automake, and I'm somewhat annoyed that my compile
I recently learned that sometimes people will lock specific processes or threads to specific
I recently learned about the right way to work with reverse iterators in C++
I recently learned that Embedded Resource means that assets are saved to an external
Even though I am a long time C programmer, I only recently learned that
UPDATE: I recently learned from this question that in the entire discussion below, I

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.