+10  A: 

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) %>
Jon Skeet
Don't I need "this HtmlHelper html", in order for it to be an extension method ?
KingNestor
Also, you mention having a single static instance isn't thread safe. Can you explain this for me? What could possibly happen with a bunch of threads potentially calling next() on that same instance?
KingNestor
@KingNestor you need that in order for it to be an extension method, but you don't need that to call public static function.
Arnis L.
@KingNestor: If you call Next from multiple threads you can corrupt the internal state of the RNG. Don't do it :)
Jon Skeet
A: 

I refer you to here and here. :-) </sarcasm>

Dan Atkinson