Why do I need to initialize a new Random () with unchecked (Environment.TickCount * 31)?

I found this instance initialization Random:

var random = new Random(unchecked(Environment.TickCount * 31));

Why not just use it new Random()?

+4
source share
1 answer

The keyword uncheckeddoes not allow an exception to be thrown when the calculation overflows Environment.TickCount * 31.

The resulting calculation is essentially a random integer (it throws out a bunch of high bits), which is used to seed the random number generator.

Please note that Reference Source for Random has this code as its constructor without parameters:

public Random() 
    : this(Environment.TickCount) {
  }
+7
source

Source: https://habr.com/ru/post/1648663/


All Articles