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

  • SEARCH
  • Home
  • 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 6102563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:37:42+00:00 2026-05-23T13:37:42+00:00

Is std::random_shuffle threadsafe? I presume not since the regular rand() is not threadsafe. If

  • 0

Is std::random_shuffle threadsafe? I presume not since the regular rand() is not threadsafe. If that is the case, how would I use rand_r with random_shuffle so that I can give each thread a unique seed. I’ve seen examples of using custom random generators with random_shuffle, but it is still unclear to me.

Thanks.

  • 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-23T13:37:43+00:00Added an answer on May 23, 2026 at 1:37 pm

    To use rand_r with std::random_shuffle, you’ll need to write a (fairly trivial) wrapper. The random number generator you pass to random_shuffle needs to accept a parameter that specifies the range of numbers to be produced, which rand_r does not.

    Your wrapper would look something like this:

    class rand_x { 
        unsigned int seed;
    public:
        rand_x(int init) : seed(init) {}
    
        int operator()(int limit) {
            int divisor = RAND_MAX/(limit+1);
            int retval;
    
            do { 
                retval = rand_r(&seed) / divisor;
            } while (retval > limit);
    
            return retval;
        }        
    };
    

    You’d use it with random_shuffle something like:

    std::random_shuffle(whatever.begin(), whatever.end(), rand_x(some_seed));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If a random generator function is not supplied to the random_shuffle algorithm in the
We are trying to use the std::deque erase member function. The return value of
I am having problems trying to serialise a vector (std::vector) into a binary format
I'm an amateur programmer that's experimenting using pthreads, to see to what extent a
i have to define a variable that i am using in a program but
Is there some function, similar to srand() , that I need to call to
I have problems using std::rotate on a const_iterator over a unique_ptr middle. I have
I have the following code: template<typename T, typename Allocator = std::allocator<T> > class Carray
I profiled my code using gprof and from the report, most, if not all
I'm writing a random access container in C++. In my code I use this

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.