System.Random is much faster in .NET 6 as Microsoft have changed underlying algorithm

In .NET 6 Microsoft have changed the algorithm used in System.Random and the performance improvements are crazy.

Check out the Ratio columns on the BenchmarkDotNet output below… WOW!!!

Click on the image for a larger view in a new tab.

There’s so many performance improvements in .NET 6 but the Random class is used in hot loops all the time so the potential here is huge…

BUT… remember for cryptographically-secure random number generation the RandomNumberGenerator class in the System.Security.Cryptography namespace should be used instead.

Leave a Reply

Your email address will not be published. Required fields are marked *