views:

87

answers:

3

What is the correct way of generating random numbers in an ASP.NET MVC application if I need exactly one number per request? According to MSDN, in order to get randomness of sufficient quality, it is necessary to generate multiple numbers using a single System.Random object, created once. Since a new instance of a controller class is created for each request in MVC, I cannot use a private field initialized in the controller's constructor for the Random object. So in what part of the MVC app should I create and store the Random object? Currently I store it in a static field of the controller class and lazily initialize it in the action method that uses it:

public class HomeController : Controller
{
    ...

    private static Random random;

    ...

    public ActionResult Download()
    {
        ...

        if (random == null)
            random = new Random();

        ...

    }
}

Since the "random" field can be accessed by multiple instances of the controller class, is it possible for its value to become corrupted if two instances attempt to initialize it simultaneously? And one more question: I know that the lifetime of statics is the lifetime of the application, but in case of an MVC app what is it? Is it from IIS startup till IIS shutdown?

A: 

You could have a static contructor in HomeController to save you having to lazy initialize it in every method. This pretty much ensures that the Random only gets initialised once (the very first time it's accessed).

public class HomeController : Controller
{
    ...

    private static Random random;

    static HomeController()
    {
        random = new Random();
    }

    ...

    public ActionResult Download()
    {
        ...

        //use random - its already created.


        ...

    }
}
Jamiec
+1  A: 

Unless you're throwing together some quick demo or something, I would put this responsibility into a service or infrastructure layer (ie, just another class) and let it manage the lifetime of your random number generator. It's not really the controller's job to manage this anyway - and you won't have worry about it when/if you have another controller that needs a random number.

James H
+3  A: 

Ideally you want to maintain an instance of the Random class for longer than the lifetime of a single page. Do not do this by putting it in a static variable; the Random class is not thread-safe and this will result in problems. From the docs:

Any instance members are not guaranteed to be thread safe.

My favourite approach is the RandomGen2 wrapper class from the Microsoft ParallelFX team (who really know what they're doing with threading) which uses an instance per thread for (mostly) lock-free and thread-safe random numbers.

public static class RandomGen2 
{ 
    private static Random _global = new Random(); 
    [ThreadStatic] 
    private static Random _local;

    public static int Next() 
    { 
        Random inst = _local; 
        if (inst == null) 
        { 
            int seed; 
            lock (_global) seed = _global.Next(); 
            _local = inst = new Random(seed); 
        } 
        return inst.Next(); 
    } 
}

Which you can then just call as follows:

var rand = RandomGen2.Next();

You may need to add extra methods to wrap the other Random methods you want to access, and I'd suggest a better name such as ThreadSafeRandom, but it demonstrates the principle.

Greg Beech