What's wrong with Random and RandomNumberGenerator?
Random is slow and not thread-safe (fails miserably and silently on concurrent access)
Random is incorrectly implemented:
Randomr=new Random();// new CryptoRandom();constintmod=2;int[]hist=newint[mod];for(inti=0;i<10000000;i++){intnum= r.Next(0x55555555);intnum2=num%2;++hist[num2];}for(inti=0;i<mod;i++)
Console.WriteLine($"{i}: {hist[i]}");// Run this on .NET 5 or below. Surprised? Now change to CryptoRandom// Fails on .NET 6 if you use seeded "new Random(seed)"
Random/.NET 6 unseeded is fast (new algorithm), with a safe .Shared property, but instances are not thread-safe
Random/.NET 6 seeded falls back to legacy slow non-thread-safe .NET algorithm
Neither Random implementation aims for cryptographically-strong results
RandomNumberGenerator can be much faster with intelligent wrapping and more useful Random API
Throughput (single-threaded) .NET 6:
BenchmarkDotNet=v0.13.1, OS=Windows 10.0.19042.1165(20H2/October2020Update)
Intel Core i7-10510U CPU 1.80GHz,1 CPU,8 logical and 4 physical cores
.NET SDK=6.0.100-preview.7.21379.14[Host]:.NET6.0.0(6.0.21.37719), X64 RyuJIT
Method
BYTES
Mean
Error
StdDev
Ratio
RatioSD
Throughput
SystemRandom
32
7.483 μs
0.2714 μs
0.0149 μs
0.21
0.00
4,176 MB/s
SystemSharedRandom
32
13.197 μs
3.3861 μs
0.1856 μs
0.38
0.00
2,368 MB/s
SeededSystemRandom
32
261.568 μs
12.8670 μs
0.7053 μs
7.46
0.04
119 MB/s
CryptoRandom
32
35.083 μs
1.6305 μs
0.0894 μs
1.00
0.00
891 MB/s
SeededCryptoRandom
32
27.551 μs
0.9383 μs
0.0514 μs
0.79
0.00
1,134 MB/s
RNG_Fill
32
106.507 μs
14.8172 μs
0.8122 μs
3.04
0.02
293 MB/s
SystemRandom
1024
132.600 μs
4.5819 μs
0.2511 μs
0.35
0.00
7,541 MB/s
SystemSharedRandom
1024
139.345 μs
28.1093 μs
1.5408 μs
0.37
0.00
7,176 MB/s
SeededSystemRandom
1024
8,260.379 μs
265.3543 μs
14.5450 μs
21.79
0.01
121 MB/s
CryptoRandom
1024
379.137 μs
8.8802 μs
0.4868 μs
1.00
0.00
2,638 MB/s
SeededCryptoRandom
1024
320.513 μs
21.3931 μs
1.1726 μs
0.85
0.00
3,120 MB/s
RNG_Fill
1024
447.914 μs
15.4506 μs
0.8469 μs
1.18
0.00
2,233 MB/s
Throughput (single-threaded) .NET 5:
BenchmarkDotNet=v0.13.1, OS=Windows 10.0.19042.1165(20H2/October2020Update)
Intel Core i7-10510U CPU 1.80GHz,1 CPU,8 logical and 4 physical cores
.NET SDK=6.0.100-preview.7.21379.14[Host]:.NET5.0.9(5.0.921.35908), X64 RyuJIT