Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to implement random number stream accross high traffic asp.net application with multiple servers
    text
    copied!<p>This question comes from the knowledge that when <code>new Random()</code> is called very quickly it gets seeded with the same value which I assume is based on <code>DateTime.Now.Ticks</code>.</p> <p>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.</p> <p>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.</p> <p>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.</p> <p>Is there a good/standard solution to this scenario?</p> <p><strong>Update: the actual scenario I'm working on (which really has no chance of getting enough volume to cause a problem)</strong></p> <p>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:</p> <pre><code>// Randomly display one of (FreeCreditScore1, MyFreeScoreNow1) private void ShowCreditScoreAd() { FreeCreditScore1.Visible = (new Random().Next(2) == 1); MyFreeScoreNow1.Visible = !FreeCreditScore1.Visible; } </code></pre> <p>If I unit test the above code by calling it in rapid succession it fails and I came to understand the reason was that <code>new Random()</code> was getting called in the same "tick".</p> <p>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...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload