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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:31:37+00:00 2026-06-05T00:31:37+00:00

I came across a strange problem today that i could understand the reason. Take

  • 0

I came across a strange problem today that i could understand the reason. Take the following console program.

internal class Program
    {
        private static void Main(string[] args)
        {
            string s1 = GenerateRandomCode(8);
            string s2 = GenerateRandomCode(8);
            string s3 = GenerateRandomCode(8);
        }

        public static string GenerateRandomCode(int length)
        {
            string charPool = "ABCDEFGOPQRSTUVWXY1234567890ZabcdefghijklmHIJKLMNnopqrstuvwxyz";
            StringBuilder rs = new StringBuilder();
            Random random = new Random();

            for (int i = 0; i < length; i++)
            {
                rs.Append(charPool[(int)(random.NextDouble() * charPool.Length)]);
            }

            return rs.ToString();
        }
    }

If i put a breakpoint at the of the program and run the program the values of s1, s2, s3 are all the equal. Now if i put a breakpoint at s2 for example the value returned will be different.

Seems like some kind of concurrency issue? What’s going on?

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-05T00:31:39+00:00Added an answer on June 5, 2026 at 12:31 am

    Random number generators are in fact not fully random: given the same seed value, multiple instances will generate the same random sequence.

    Couldn’t say it better than the MSDN documentation for the Random constructor

    “The default seed value is derived from the system clock and has
    finite resolution. As a result, different Random objects that are
    created in close succession by a call to the default constructor will
    have identical default seed values and, therefore, will produce
    identical sets of random numbers. This problem can be avoided by using
    a single Random object to generate all random numbers. You can also
    work around it by modifying the seed value returned by the system
    clock and then explicitly providing this new seed value to the
    Random(Int32) constructor. For more information, see the Random(Int32)
    constructor.”

    So, in your case you need to keep the Random instance as a class-level field or a function parameter, and instantiate it only once.

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

Sidebar

Related Questions

I came across a strange problem today. I created a subclass of UIView and
I came across a strange phenomena upon running the following code: #include <iostream> class
Today, I came across quite strange problem. I needed to calculate string length of
I came across a rather strange problem with linq-to-sql. In the following example, var
Today I came across the strange ruby syntax in the Rational class: Rational(a,b) (Notice
I came accross a very strange problem with tr1::regex (VS2008) that I can't figure
I came across a strange behaviour with Chrome earlier that I can't seem to
I came across some Java code that has a method containing the following: static
today I came a across a pretty strange behaviour of an php based application
Today i'm cutting video at work (yea me!), and I came across a strange

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.