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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:07:24+00:00 2026-05-16T21:07:24+00:00

The accepted answer to this question , and a similar discussion at work today

  • 0

The accepted answer to this question, and a similar discussion at work today got me to wondering about something.

The question was about how to safely generate random numbers in a multi-threaded program. The accepted answer advocates using thread local storage, effectively creating one random number generator per thread. I wonder if that’s really such a good idea.

Let’s say we have two threads start up at the same time (quite possible on a multi-core system) and both call the default Random constructor to create and initialize a random number generator in thread local storage. Since they didn’t pass a seed parameter, Random uses the system time as the seed. So both random number generators have been initialized with the same seed. They will both generaete the same sequence of random numbers.

Since these threads are allocated from the thread pool, there’s no way you can associate a particular object with a particular thread. Or, in the case of the above-referenced question, you can’t guarantee which pool thread will execute the next request. So imagine that the following happens:

At startup, two requests come in simultaneously.
Two threads are created, each initializing a random number generator with the same seed.

Each thread generates three random numbers.  They will be identical in both threads.

Next request comes in.  It's assigned to thread #1.
It generates a random number and exits.

Some period of time elapses.

Next request comes in.  It's assigned to thread #2.
It generates the same random number that thread #1 did just a while ago.

This could continue indefinitely, although I doubt it would be ping-ponging quite that badly. The point is that both threads have the same PRNG and the likelihood of repeating a sequence is very high. I understand that the P in PRNG stands for “pseudo”, but this is a bit much.

I think it’s quite possible for multiple threads to initialize a Random instance with the same seed value. If that happens, then the “randomness” of at least some things in the application is going to suffer. The implications of that, of course, are application dependent.

What I don’t know is, if the PRNGs are initialized with different seeds, does that make the sequence seen by a client more random, less random, or about the same? That is, if I were to write:

var rnd1 = new Random(123);
var rnd2 = new Random(654);
for (int i = 0; i < OneMillion; ++i)
{
    numbers.Add(rnd1.Next());
 numbers.Add(rnd2.Next());
}

Would the sequence of numbers I generate be more or less random than if I were to just generate two million from either of the PRNGs?

  • 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-16T21:07:24+00:00Added an answer on May 16, 2026 at 9:07 pm

    The level of randomness should be approximately the same, since both series are generated by the same algorithm.

    How do you define randomness? Whether one series appears more random or not may well depend on the user, and what the app does with the series of numbers.

    If you’re worried about the same seed being used for multiple random number generators, you could always seed all your random number generators from the sequence generated by another, single generator. That way, at least your initial starting point is somewhat arbitrary.

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

Sidebar

Related Questions

I just read this question and the accepted answer: What is JavaScript garbage collection?
I just read the accepted answer of this question , which left me with
This is related to the accepted answer for What’s your #1 way to be
The accepted answer to the question C++ Library for image recognition: images containing words
In another question, the accepted answer shows a method for reading the contents of
My question is similar to this MySQL question, but intended for SQL Server: Is
I am having a problem very similar to this question here: Can I use
This question is related to Microsoft ReportViewer: Session Expired Errors . Unfortunately, the accepted
EDIT Public health warning - this question includes a false assumption about undefined behaviour.
I realize that there was a similar question asked here , but this is

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.