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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:09:32+00:00 2026-05-27T22:09:32+00:00

I am trying to produce random numbers for each thread to use, but all

  • 0

I am trying to produce random numbers for each thread to use, but all the threads produce the same number.

The number changes when I run my program each time, but all the threads produce the same number for a given run.

What should I do to produce different random numbers for each thread?

void *Customer(void *customer_id)
{
    unsigned int iseed = (unsigned int)time(NULL);
    srand (iseed);
    int rastgele = rand() % 3 + 1;
    int *id_ptr,customer_idd;
    id_ptr=(int *) customer_id;
    customer_idd=*id_ptr;

    printf("This is thread : %d  %d \n",customer_idd,rastgele);
    pthread_exit(NULL);
}
  • 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-27T22:09:32+00:00Added an answer on May 27, 2026 at 10:09 pm

    You shouldn’t use rand for pseudo random generation in connection with threads. This function uses a shared state that is common to all threads. This creates dependencies between the PRN drawn by the threads and slows down substantially since the access to the state must be mutexted.

    Alternatives on POSIX systems would be nrand48 and jrand48 that receive a state (that should be thread specific) as their argument. As others said, seeding that state just with a time value is not a good idea, threads may do that at the same moment.

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

Sidebar

Related Questions

I am trying to produce true random number in c++ with C++ TR1. However,
i am trying to generate random number for my mental math quiz game. But
I'm trying to use the following code to grab a random mobile phone number
I'm trying to produce random integers (uniformly distributed). I found this snippet on an
I'm trying to produce yet another lightbox as much needed HTML/CSS/Javascript practice, but I've
I'm trying to produce a report that shows, for each Part No, the results
I'm trying to produce a list of all the possible ip addresses for a
I'm trying to produce a kind of lucky dip lotto number generator. At the
I have a float and I am trying to get a random number between
I am trying to write a simple Gui to produce a random password. The

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.