forked from Elfocrash/L2dotNET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL2Random.cs
More file actions
75 lines (67 loc) · 2.23 KB
/
Copy pathL2Random.cs
File metadata and controls
75 lines (67 loc) · 2.23 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System;
namespace L2dotNET.Utility
{
/// <summary>
/// Provides common randomization methods.
/// </summary>
public static class L2Random
{
/// <summary>
/// Internal <see cref="Random"/> object.
/// </summary>
private static readonly Random MRandom = new Random((int)DateTime.Now.Ticks);
/// <summary>
/// Returns random <see cref="int"/> value.
/// </summary>
/// <returns>Random <see cref="int"/> value.</returns>
public static int Next()
{
return MRandom.Next();
}
/// <summary>
/// Returns random <see cref="int"/> value.
/// </summary>
/// <param name="max">Max result value.</param>
/// <returns>Random <see cref="int"/> value.</returns>
public static int Next(int max)
{
return MRandom.Next(0, max);
}
/// <summary>
/// Returns randomly generated array of <see cref="byte"/> values.
/// </summary>
/// <param name="count">Array length.</param>
/// <returns>Randomly generated array of <see cref="byte"/> values.</returns>
public static byte[] NextBytes(int count)
{
byte[] buffer = new byte[count];
return NextBytes(ref buffer);
}
/// <summary>
/// Returns randomly generated array of <see cref="byte"/> values.
/// </summary>
/// <param name="buffer">Array of <see cref="byte"/> values to randomize.</param>
/// <returns>Randomly generated array of <see cref="byte"/> values.</returns>
public static unsafe byte[] NextBytes(ref byte[] buffer)
{
int i = buffer.Length,
j = 0;
fixed (byte* buf = buffer)
{
int k;
while (j <= (i - sizeof(int)))
{
k = MRandom.Next();
*(int*)(buf + j) = *&k;
j += sizeof(int);
}
while (j != i)
{
k = MRandom.Next();
*(buf + j) = *((byte*)&k + (j++ % sizeof(int)));
}
}
return buffer;
}
}
}