Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random number generation in MVC applications

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?

like image 930
SlimShaggy Avatar asked May 17 '10 07:05

SlimShaggy


1 Answers

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.

like image 195
Greg Beech Avatar answered Sep 27 '22 21:09

Greg Beech