-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathRandom.linq
More file actions
47 lines (35 loc) · 759 Bytes
/
Random.linq
File metadata and controls
47 lines (35 loc) · 759 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<Query Kind="Program">
<Namespace>System.Security.Cryptography</Namespace>
</Query>
void Main()
{
//This is not Thread safe.
(int x, int y) = (0, 10);
var random = new Random();
//Get Random number within range
random.Next(x, y)
.Dump();
//Range of max - min int values (2147483647)
random.Next()
.Dump();
var threadRandom = new TSRandom();
int bv = threadRandom.Next();
bv.Dump();
}
//ThreadSafe random
public class TSRandom
{
[ThreadStatic]
private static Random? _local;
public int Next()
{
if (_local == null)
{
byte[] buffer = new byte[4];
using var rng = RandomNumberGenerator.Create();
rng.GetBytes(buffer);
_local = new Random(BitConverter.ToInt32(buffer, 0));
}
return _local.Next();
}
}