I found this initialization of a Random
instance:
var random = new Random(unchecked(Environment.TickCount * 31));
Why not simply use new Random()
?
The keyword unchecked
prevents an exception from being thrown when the calculation Environment.TickCount * 31
integer overflows.
The resulting calculation is essentially a random integer (it throws away a bunch of high-order bits), which is used to seed the random number generator.
Note that the Reference Source for Random has this code as its parameterless constructor:
public Random()
: this(Environment.TickCount) {
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With