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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:27:00+00:00 2026-05-22T16:27:00+00:00

I have several threads which all run the same function. In each of these

  • 0

I have several threads which all run the same function. In each of these they generate a different random number several times. We tried to do this by putting srand(time(0)) at the start of the function, but it seems that they all get the same number.

Do we need to call srand(time(0)) only once per program, i.e at the start of main (for example), at the start of each function that is called several times, or something else?

  • 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-22T16:27:01+00:00Added an answer on May 22, 2026 at 4:27 pm

    srand() seeds the random number generator. You should only have to call srand(time(NULL)) once during startup.

    That said, the documentation states:

    The function rand() is not reentrant
    or thread-safe
    , since it uses hidden
    state that is modified on each call.
    This might just be the seed value to
    be used by the next call, or it might
    be something more elaborate. In order
    to get reproducible behaviour in a
    threaded application, this state must
    be made explicit. The function
    rand_r() is supplied with a pointer to
    an unsigned int, to be used as state.
    This is a very small amount of state,
    so this function will be a weak
    pseudo-random generator. Try
    drand48_r(3) instead.

    The emphasized part of the above is probably the reason why all your threads get the same number.

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

Sidebar

Related Questions

I have several modifying threads and some reading threads, which all access the global
I have a Map which is to be modified by several threads concurrently. There
We have several jobs that run concurrently that have to use the same config
I'm creating an console application which needs to run several threads in order to
We are developing a Java application with several worker threads. These threads will have
I have several threads running for an almost infinite time and number of iteration.
I have a program which uses an io_service and several threads. It instantiates some
I have a situation which involves several threads simultaneously populating a database with data
Where is the TickCount() call? I have seen several threads on the web that
I have a queue structure that is being used by several pthreads. The threads

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.