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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:00:21+00:00 2026-06-18T04:00:21+00:00

I have a program which use pthreads. In each thread a random number is

  • 0

I have a program which use pthreads. In each thread a random number is generated using the rand() (stdlib.h) function. But it seems like every thread is generating the same random number. What is the reason for that??.. Am I doing something wrong??.. 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-06-18T04:00:23+00:00Added an answer on June 18, 2026 at 4:00 am

    rand() is pseudo-random and not guaranteed to be thread-safe, regardless, you need to seed rand():

    std::srand(std::time(0)); // use current time as seed for random generator
    

    See std::rand() at cppreference.com for more details.

    A sample program may look like this:

    #include <cstdlib>
    #include <iostream>
    #include <boost/thread.hpp>
    
    boost::mutex output_mutex;
    
    void print_n_randoms(unsigned thread_id, unsigned n)
    {
        while (n--)
        {
            boost::mutex::scoped_lock lock(output_mutex);
            std::cout << "Thread " << thread_id << ": " << std::rand() << std::endl;
        }
    }
    
    int main()
    {
        std::srand(std::time(0));
        boost::thread_group threads;
        for (unsigned thread_id = 1; thread_id <= 10; ++thread_id)
        {
            threads.create_thread(boost::bind(print_n_randoms, thread_id, 100));
        }
        threads.join_all();
    }
    

    Note how the pseudo-random number generator is seeded with the time only once (and not per thread).

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

Sidebar

Related Questions

I have program which has servers interacting with each other using Twisted's remote procedure
I have my own installer program which I use to install several applications I
I have a program which creates multiple threads and each one of them tries
I have program.cpp which contains main function. There is a class Tracker which is
I have a C program in which I use pthread. I would like newly
I have a program which would use the Application Role to write data to
I have a simple program which creates a thread, and waits when this thread
I have developed a program which makes use of serial programming to read and
I have a program which is configured by the user by using C++ classes
I have a program which I want to use keybindings: // Imports.. public class

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.