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

  • Home
  • SEARCH
  • 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 7612123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:49:52+00:00 2026-05-31T01:49:52+00:00

This is some sample test app i trying to learn Parallel.Foreach Loop functionality static

  • 0

This is some sample test app i trying to learn Parallel.Foreach Loop functionality

static void Main(string[] args)
{
   List<string> Months = new List<string>()
   {
      "Jan", "Feb", "Mar", "Apr", "May", "June" 
   };
   Parallel.ForEach(Months, (x) => ProcessRandom(x));
   Console.ReadLine();
}

public static void ProcessRandom(string s)
{
     Random r = new Random();
     int i = r.Next(1, 100);
     Thread.Sleep(1000);
     Console.WriteLine(string.Format("Month Name {0} and Random ID assigned {1}", s, i));
}

My understanding about foreach in parallel is that it will run the ProcessRandom method with a parameter from the list in parallel. All the variables inside that method will be seperate and they will run independant of one another. But when i run i see that the random value stored in integer ‘i’ shows same for couple of enteries from the months list and one or may be 2 will have different random values. Why is this behaving like that. Shouldn’t the parallel loop creating new random value for each iteration. If i use parallelOptions and set MaxDegree of Parallelism to 1 then i see different values in variable ‘i’

Please guide me in understanding of parallel loops.

  • 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-31T01:49:54+00:00Added an answer on May 31, 2026 at 1:49 am

    This happens because Random isn’t really random but pseudo-random (http://msdn.microsoft.com/en-us/library/system.random.aspx). This exact issue is mentioned in the remarks of the MSDN article. When multiple instances are created at the same time and .Next() is call on both instances, it is possible they will end up with the same result (due to finite system clock resolution). To deal with this we should create a unique seed for each iteration. To that end you could do something like this (but there are many ways to skin this cat):

    static void Main(string[] args)
    {
        List<string> Months = new List<string>() { "Jan", "Feb", "Mar", "Apr", "May", "June" };
        Parallel.ForEach(Months, (x) => ProcessRandom(x));
        Console.ReadLine();
    }
    
    public static void ProcessRandom(string s)
    {            
        Random r = new Random(s.GetHashCode());
        int i = r.Next(1, 100);
        Thread.Sleep(1000);
        Console.WriteLine(string.Format("Month Name {0} and Random ID assigned {1}", s, i));
    }
    

    ** Note that this example does not guarantee uniqueness but is just shown as a way to generate a seeded value **

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

Sidebar

Related Questions

<p title=The test paragraph>This is a sample of some <b>HTML you might<br>have</b> in your
I'm trying to test the functionality of a web app by scripting a login
Can someone write some sample code to explain this concept? I know what a
I don't know how to explain this correctly but just some sample for you
I'm teaching myself Python and I was translating some sample code into this class
I've written this sample code to print the length of x. But for some
I'm building a simple app and trying to test DB result output. But unfortunately
I am trying to make my app rotation friendly, but I am having some
I'm trying to upload some data to my App Engine datastore using the bulkuploader.
I'm trying to get along with Rails 3 and some Ajaxification of my app

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.