As ever with this sort of issue, the problem is that you're creating a new Random
instance on every iteration.
Create a single instance and reuse it repeatedly. You can use a static variable, but that won't be thread-safe. In this particular case, creating a new instance per page would probably be okay. However, you'll still get the same data if two people access the page at the same time.
Ideally, you could create a static random used in a thread-safe way, to create new instances of Random which can then be used without locking within a single thread. For example:
public static class RandomFactory
{
private static Random rng = new Random();
private static readonly object padlock = new object();
public static Random CreateRandom()
{
lock (padlock)
{
return new Random(rng.Next());
}
}
}
Then in your page you could have:
// Instance variable
protected readonly Random rng = RandomFactory.CreateRandom();
and change your method to:
public static string GetRandomNumber(this HtmlHelper html, Random rng,
int low, int high)
{
return rng.Next(low, high).ToString();
}
(I'm not quite sure why you've got HtmlHelper
at all there, to be honest - you're not using it...)
and finally your mark-up to things like this:
<%= Html.GetRandomNumber(rng, 0, 30) %>