An oft-found trap ;-p
Just for completeness, Jon Skeet has a StaticRandom class in one of
his tool-bags which may be of interest to anyone using occasional
random numbers (useful where this type of private Random instance is
inconvenient due to e.g. threading / scoping constraints). Assuming
your code is not threaded, then for this type of tight loop I would
recommend the solution already posted (a "local" fixed instance),
purely to avoid the overhead of locking etc associated with static
utilities; OK, an uncontested lock is still fast, but not locking at
all (where it isn't necessary) is faster. If your code *is* threaded,
then you should probably note the risk of two calls to the *same*
Random.Next at the same time, which may not be safe; in this case you
could either:
* do your own local locking in the static method
* use StaticRandom
* (my preferred option) give each thread a separate *instance* of the
DiceRoller class (or whatever), so that they don't conflict and each
has an uncontested (instance) _rand that doesn't need locking.
In the general case, static methods should be thread safe. Looking at
http://msdn2.microsoft.com/en-us/library/2dx6wyd4.aspx, it doesn't
make any claims about thread-safety, so I have to assume that it isn't
thread-safe, which means the code I posted also isn't. Oops.
http://www.yoda.arachsys.com/csharp/miscutil/
http://www.yoda.arachsys.com/csharp/threads/
Marc