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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T03:44:18+00:00 2026-06-19T03:44:18+00:00

This question comes from the knowledge that when new Random() is called very quickly

  • 0

This question comes from the knowledge that when new Random() is called very quickly it gets seeded with the same value which I assume is based on DateTime.Now.Ticks.

Suppose you had a high traffic web application on latest versions ASP.NET, IIS, .NET, etc.. which implements an online casino. I would think the simulated slot machines need to pull from a single random number stream.

With high volume situation you might get two slot machines with the same random number generator causing too many big jackpots. I don’t have a full understanding of pseudorandom generators but my intuition is that to safely implement an online casino you really need to pull from a single generator that is seeded just once.

One solution I can think of is a synchronized queue with a single thread that pushes the numbers on, but I wouldn’t know how to synchronize it across a multi-site application.

Is there a good/standard solution to this scenario?

Update: the actual scenario I’m working on (which really has no chance of getting enough volume to cause a problem)

My real-world situation is that I have a moderately high traffic asp.net web forms site and the requirement to show one of two user controls on each request. I did so with the following code:

// Randomly display one of (FreeCreditScore1, MyFreeScoreNow1)
private void ShowCreditScoreAd()
{
    FreeCreditScore1.Visible = (new Random().Next(2) == 1);
    MyFreeScoreNow1.Visible = !FreeCreditScore1.Visible;
}

If I unit test the above code by calling it in rapid succession it fails and I came to understand the reason was that new Random() was getting called in the same “tick”.

That said, I do believe my current implementation is sufficient (feel free to correct me) but I was curious how it could be solved if I really wanted to be strict about it…

  • 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-19T03:44:20+00:00Added an answer on June 19, 2026 at 3:44 am

    Just protect the random number generator with a lock. Unless you’re making hundreds of thousands of calls per second to it, it’s going to be plenty fast enough.

    public class MyRandomObject
    {
        private readonly Random _rnd = new Random();
    
        public int Next()
        {
            lock (_rnd)
            {
                return _rnd.Next();
            }
        }
    }
    

    Before you say, “locks are too slow,” note that I tested this on an old 2.4 GHz quad core. It took approximately 70 nanoseconds to get a random number when the lock isn’t contended. Lock contention can kill performance, but you’d need lots of requests.

    In a Web app, you’d want to initialize one of those in Application_Start, and make that singleton available to the rest of your application.

    There are faster ways, but they’re more difficult to implement. One way would be to pre-generate millions of random numbers (call Random.NextBytes) and store them in a buffer. Protect that with a reader/writer lock. When the number of values remaining reaches some threshold, the thread grabs the writer lock, preventing all other access until it refills the buffer. This is probably the way you’d want to go if you’re using some other source of random numbers. For example the RNGCryptoServiceProvider.GetBytes method (http://msdn.microsoft.com/en-us/library/system.security.cryptography.rngcryptoserviceprovider.getbytes.aspx).

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

Sidebar

Related Questions

This question comes from breaking up a question that was too big. Users create
Check this example before reading the question - http://www.sqlfiddle.com/#!2/fcf3e/8 The following data comes from
This question has been puzzling me for a long time now. I come from
I come from Windows .Net forms development. This is a pretty basic/fundamental question. I'm
This may be a bit of daft question, but I don't come from an
Okay, this question comes through a friend so it might be lost in translation...
( Late edit: This question will hopefully be obsolete when Java 7 comes, because
I'm new to blackberry development. There is this question I've come across several times,
I know that my this question may sound pathetic to you but as a
I'll say up front that this question is similar in nature to this .

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.