You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Random Extensions is a library that provides functionality for pseudorandom number generation for .NET and Unity.
12
+
NRandom is a library that provides functionality for pseudorandom number generation for .NET and Unity.
12
13
13
14
.NET has a built-in `Random` class, but it is not sufficient in terms of functionality and contains complex implementations and unnecessary abstractions due to compatibility issues.
14
15
15
16
Unity's `UnityEngine.Random` is a static class, making it impossible to instantiate. Additionally, it manages internal states, making it difficult to reproduce random numbers.
16
17
17
-
Random Extensions introduces `IRandom` as a new abstraction layer for random number generation, providing high performance implementations based on various algorithms (xoshift, xoshiro, splitmix, PCG). It also offers many useful features for handling random numbers, such as extension methods that support `System.Numerics` and Unity types, `IWeightedCollection<T>` for handling weighted random numbers, and LINQ extensions for random numbers (`RandomEnumerable`).
18
+
NRandom introduces `IRandom` as a new abstraction layer for random number generation, providing high performance implementations based on various algorithms (xoshift, xoshiro, splitmix, PCG). It also offers many useful features for handling random numbers, such as extension methods that support `System.Numerics` and Unity types, `IWeightedCollection<T>` for handling weighted random numbers, and LINQ extensions for random numbers (`RandomEnumerable`).
18
19
19
20
> [!WARNING]
20
21
> Do not use this library for security purposes. If you need cryptographically secure random numbers, use `System.Security.Cryptography.RandomNumberGenerator`.
21
22
23
+
> [!NOTE]
24
+
> For migration from RandomExtensions(v1), please see [here](./docs/migrating_randomextensions.md).
25
+
22
26
## Installation
23
27
24
28
### NuGet packages
25
29
26
-
Random Extensions requires .NET Standard 2.1 or higher. The package is available on NuGet.
30
+
NRandom requires .NET Standard 2.1 or higher. The package is available on NuGet.
27
31
28
32
### .NET CLI
29
33
30
34
```ps1
31
-
dotnet add package RandomEx
35
+
dotnet add package NRandom
32
36
```
33
37
34
38
### Package Manager
35
39
36
40
```ps1
37
-
Install-Package RandomEx
41
+
Install-Package NRandom
38
42
```
39
43
40
44
### Unity
41
45
42
-
You can use Random Extensions in Unity by using NugetForUnity. For more details, refer to the [Unity](#unity-1) section.
46
+
You can use NRandom in Unity by using NugetForUnity. For more details, refer to the [Unity](#unity-1) section.
43
47
44
48
## Basic Usage
45
49
46
50
You can generate random numbers using `RandomEx.Shared`.
47
51
48
52
```cs
49
-
usingRandomExtensions;
53
+
usingNRandom;
50
54
51
55
// Get a random value between 0-9
52
56
varn=RandomEx.Shared.NextInt(0, 10);
@@ -70,7 +74,7 @@ var d = rand.NextDouble();
70
74
71
75
## Supported Types
72
76
73
-
Random Extensions supports more types than the standard `System.Random`.
77
+
NRandom supports more types than the standard `System.Random`.
74
78
75
79
```cs
76
80
varrand=RandomEx.Create();
@@ -112,37 +116,7 @@ rand.NextBytes(buffer); // Fill the buffer with random bytes
112
116
113
117
Additionally, by introducing the extension package, you can use methods that support types in `System.Numerics` and Unity. For details, see the sections on [System.Numerics](#systemnumerics) and [Unity](#unity-1).
114
118
115
-
## Collection Operations
116
-
117
-
### Element Retrieval
118
-
119
-
You can use the `GetItem()` method to retrieve a random element from an array. If you want to retrieve multiple elements at once, use `GetItems()`.
Below is a sample of using `WeightedList<T>` for weighted selection.
@@ -214,19 +187,32 @@ Below is a sample of using `WeightedList<T>` for weighted selection.
214
187
varweightedList=newWeightedList<string>();
215
188
216
189
// Add elements with specified weights
217
-
weightedList.Add("Legendary", 0.5f);
218
-
weightedList.Add("Epic", 2.5f);
219
-
weightedList.Add("Rare", 12f);
220
-
weightedList.Add("Uncommon", 25f);
221
-
weightedList.Add("Common", 60f);
190
+
weightedList.Add("Legendary", 0.5);
191
+
weightedList.Add("Epic", 2.5);
192
+
weightedList.Add("Rare", 12);
193
+
weightedList.Add("Uncommon", 25);
194
+
weightedList.Add("Common", 60);
222
195
223
196
// Retrieve a weighted random element
224
-
varrarity=weightedList.GetItem();
197
+
varrarity=weightedList.GetRandom();
198
+
```
199
+
200
+
You can also perform a weighted random draw without duplicates using `RemoveRandom()`.
201
+
202
+
```cs
203
+
varlist=newWeightedList<string>();
204
+
list.Add("Foo", 1.0);
205
+
list.Add("Bar", 1.5);
206
+
list.Add("Baz", 3.0);
207
+
208
+
list.RemoveRandom(outvaritem0);
209
+
list.RemoveRandom(outvaritem1);
210
+
list.RemoveRandom(outvaritem2);
225
211
```
226
212
227
213
## IRandom
228
214
229
-
Random Extensions provides `IRandom` as an interface for random number generators. By implementing this interface, you can create custom random number generator.
215
+
NRandom provides `IRandom` as an interface for random number generators. By implementing this interface, you can create custom random number generator.
230
216
231
217
```cs
232
218
publicinterfaceIRandom
@@ -239,29 +225,36 @@ public interface IRandom
239
225
240
226
### IRandom Implementations
241
227
242
-
Random Extensions provides several `IRandom` implementations by default. Below is a list of class names and the pseudorandom number algorithms they use internally.
228
+
NRandom provides several `IRandom` implementations by default. Below is a list of class names and the pseudorandom number algorithms they use internally.
2. Open the NuGet window by selecting `NuGet > Manage NuGet Packages`, search for the `RandomEx` package, and install it.
327
+
2. Open the NuGet window by selecting `NuGet > Manage NuGet Packages`, search for the `NRandom` package, and install it.
335
328

336
329
337
330
3. Open the Package Manager window by selecting `Window > Package Manager`, then click on `[+] > Add package from git URL` and enter the following URL:
0 commit comments