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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:04:23+00:00 2026-05-15T17:04:23+00:00

Possible Duplicate: System.Random keeps on returning the same value I’m refactoring and expanding a

  • 0

Possible Duplicate:
System.Random keeps on returning the same value

I’m refactoring and expanding a small C# agent-based model to help some biology professors predict the spread of a disease. Each year of the simulation each individual agent randomly travels to a nearby population node, possibly spreading the disease. I’m brand new to C#, but I’ve read about potential problems with Random.Next returning the same value if re-initialized with the same system time. To avoid this I’ve created a static instance which is referenced for each new random value.

The specifics:

In my efforts to scale up the model I’ve changed it to compute the “travel” information for each population node in parallel. When testing the model I noticed that in the new version the disease would not spread past the first year. Further inquiry narrowed the problem down to the travel between nodes. After the first year all the agents remained immobile. I examined the function responsible for their travel and found that it works by creating a list of all nearby nodes, generating a random number <= the number of elements in the list, and travelling to listOfNearbyNodes[myRandomNumber].

The problem:

I then added a print statement to output the value of the random index for each iteration. I found that the whole model works exactly as expected for the first year of the simulation, with the random numbers being generated in an acceptable range. However, after the first year ends and the simulation loops the exact same code will only return a “random” index of 0. Every thread, every iteration, every node, every agent, always 0. As the current node of an agent is always the first item in the list the agents never move again.

I thought this might be another manifestation of the system time seed error, so I’ve tried three different ways of implementing a static random object, but it doesn’t help. Every time I run the simulation the first year always works correctly and then Random.Next() starts to only return 0’s.

Does anyone have ideas as to where I should look next for the bug? 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-05-15T17:04:23+00:00Added an answer on May 15, 2026 at 5:04 pm

    I suspect you’re using the same instance of Random concurrently in multiple threads. Don’t do that – it’s not thread-safe.

    Options:

    • Create a new instance of Random per thread (ThreadStatic can help here)
    • Use a single instance of Random, but only ever use it in a lock.

    I have a blog post with some sample code, but please read the comments as well as there are good suggestions for improving it. I’m planning on writing another article about randomness at some point in the near-ish future…

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

Sidebar

Ask A Question

Stats

  • Questions 483k
  • Answers 483k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You didn't submitted the form elements' name's only their id's.… May 16, 2026 at 7:02 am
  • Editorial Team
    Editorial Team added an answer Using Word Automation one way of targetting ContentControls in the… May 16, 2026 at 7:02 am
  • Editorial Team
    Editorial Team added an answer You need Math.toDegrees(Math.atan((y2-y1)/(x2-x1))) Do notice exception when x1=x2. May 16, 2026 at 7:02 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.