Skip to content

Commit cad8730

Browse files
committed
Use Random as a singleton.
1 parent 3a00031 commit cad8730

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

EncryptionTests/TestUtilities.cs

+20-13
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,41 @@ namespace EncryptionTests
77
[TestClass]
88
internal static class TestUtilities
99
{
10-
public static string GeneratePassPhrase()
10+
private static Random rand;
11+
12+
private static Random Rand
1113
{
12-
Random rand = new Random();
14+
get
15+
{
16+
if (rand == null)
17+
{
18+
rand = new Random();
19+
}
1320

14-
return GenerateRandomString(rand.Next(8, (256 + 1)));
21+
return rand;
22+
}
1523
}
1624

17-
public static byte[] GeneratePayload()
25+
public static string GeneratePassPhrase()
1826
{
19-
Random rand = new Random();
27+
return GenerateRandomString(Rand.Next(8, (256 + 1)));
28+
}
2029

21-
return GenerateRandomBytes(rand.Next(16, (512 + 1)));
30+
public static byte[] GeneratePayload()
31+
{
32+
return GenerateRandomBytes(Rand.Next(16, (512 + 1)));
2233
}
2334

2435
private static string GenerateRandomString(int length)
2536
{
2637
byte[] bytes = new byte[length];
2738

28-
Random rand = new Random();
29-
3039
for (int i = 0; i < bytes.Length; i++)
3140
{
3241
int min;
3342
int max;
3443

35-
if (rand.Next(0, 2) > 0)
44+
if (Rand.Next(0, 2) > 0)
3645
{
3746
min = 65;
3847
max = 90;
@@ -43,7 +52,7 @@ private static string GenerateRandomString(int length)
4352
max = 122;
4453
}
4554

46-
int num = rand.Next(min, max);
55+
int num = Rand.Next(min, max);
4756

4857
bytes[i] = (byte)num;
4958
}
@@ -53,11 +62,9 @@ private static string GenerateRandomString(int length)
5362

5463
private static byte[] GenerateRandomBytes(int length)
5564
{
56-
Random rand = new Random();
57-
5865
byte[] bytes = new byte[length];
5966

60-
rand.NextBytes(bytes);
67+
Rand.NextBytes(bytes);
6168

6269
return bytes;
6370
}

0 commit comments

Comments
 (0)