Skip to content

Commit 89d3166

Browse files
authored
Merge pull request #13 from nuskey8/NRandom
Move to NRandom
2 parents b635a9c + de1489f commit 89d3166

File tree

170 files changed

+1739
-1182
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

170 files changed

+1739
-1182
lines changed

Directory.Build.props

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22

33
<PropertyGroup>
44
<!-- NuGet Packaging -->
5-
<PackageVersion>1.0.4</PackageVersion>
6-
<Authors>Annulus Games</Authors>
7-
<CopyrightAnnulus Games</Copyright>
8-
<PackageProjectUrl>https://github.com/AnnulusGames/RandomExtensions</PackageProjectUrl>
5+
<PackageVersion>2.0.0</PackageVersion>
6+
<Authors>nuskey</Authors>
7+
<CopyrightYusuke Nakada</Copyright>
8+
<PackageProjectUrl>https://github.com/nuskey8/NRandom</PackageProjectUrl>
99
<RepositoryUrl>$(PackageProjectUrl)</RepositoryUrl>
1010
<RepositoryType>git</RepositoryType>
1111
<PackageLicenseExpression>MIT</PackageLicenseExpression>
1212
<PackageReadmeFile>README.md</PackageReadmeFile>
13-
<PackageIcon>Icon.png</PackageIcon>
1413
</PropertyGroup>
1514

1615
</Project>

Icon.png

-10.3 KB
Binary file not shown.

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2024 Annulus Games
3+
Copyright (c) 2025 Yusuke Nakada
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

NRandom.slnx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Solution>
2+
<Folder Name="/sandbox/">
3+
<Project Path="sandbox/Benchmark/Benchmark.csproj" />
4+
<Project Path="sandbox/ConsoleApp1/ConsoleApp1.csproj" />
5+
</Folder>
6+
<Folder Name="/src/">
7+
<Project Path="src/NRandom.Numerics/NRandom.Numerics.csproj" />
8+
<Project Path="src/NRandom/NRandom.csproj" />
9+
</Folder>
10+
<Folder Name="/tests/">
11+
<Project Path="tests/NRandom.Tests/NRandom.Tests.csproj" />
12+
</Folder>
13+
</Solution>

README.md

Lines changed: 86 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,56 @@
1-
# Random Extensions
1+
# NRandom
2+
3+
[![NuGet](https://img.shields.io/nuget/v/NRandom.svg)](https://www.nuget.org/packages/NRandom)
4+
[![Releases](https://img.shields.io/github/release/nuskey8/NRandom.svg)](https://github.com/nuskey8/NRandom/releases)
5+
[![GitHub license](https://img.shields.io/github/license/nuskey8/NRandom.svg)](./LICENSE)
26

3-
[![NuGet](https://img.shields.io/nuget/v/RandomEx.svg)](https://www.nuget.org/packages/RandomEx)
4-
[![Releases](https://img.shields.io/github/release/AnnulusGames/RandomExtensions.svg)](https://github.com/AnnulusGames/RandomExtensions/releases)
5-
[![GitHub license](https://img.shields.io/github/license/AnnulusGames/RandomExtensions.svg)](./LICENSE)
67

78
English | [日本語](README_JA.md)
89

910
## Overview
1011

11-
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.
1213

1314
.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.
1415

1516
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.
1617

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`).
1819

1920
> [!WARNING]
2021
> Do not use this library for security purposes. If you need cryptographically secure random numbers, use `System.Security.Cryptography.RandomNumberGenerator`.
2122
23+
> [!NOTE]
24+
> For migration from RandomExtensions(v1), please see [here](./docs/migrating_randomextensions.md).
25+
2226
## Installation
2327

2428
### NuGet packages
2529

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.
2731

2832
### .NET CLI
2933

3034
```ps1
31-
dotnet add package RandomEx
35+
dotnet add package NRandom
3236
```
3337

3438
### Package Manager
3539

3640
```ps1
37-
Install-Package RandomEx
41+
Install-Package NRandom
3842
```
3943

4044
### Unity
4145

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.
4347

4448
## Basic Usage
4549

4650
You can generate random numbers using `RandomEx.Shared`.
4751

4852
```cs
49-
using RandomExtensions;
53+
using NRandom;
5054

5155
// Get a random value between 0-9
5256
var n = RandomEx.Shared.NextInt(0, 10);
@@ -70,7 +74,7 @@ var d = rand.NextDouble();
7074
7175
## Supported Types
7276

73-
Random Extensions supports more types than the standard `System.Random`.
77+
NRandom supports more types than the standard `System.Random`.
7478

7579
```cs
7680
var rand = RandomEx.Create();
@@ -112,37 +116,7 @@ rand.NextBytes(buffer); // Fill the buffer with random bytes
112116

113117
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).
114118

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()`.
120-
121-
```cs
122-
var rand = RandomEx.Create();
123-
124-
// Create an array of values
125-
var values = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
126-
127-
// Get a random element
128-
var item = rand.GetItem(values);
129-
130-
// Get 5 random elements (with duplicates)
131-
var items = rand.GetItems(values, 5);
132-
```
133-
134-
You can also specify weights for each element. The array of weights you pass as an argument must match the number of elements in the original array.
135-
136-
```cs
137-
var values = new int[] { 0, 1, 2 };
138-
var weights = new double[] { 1.0, 5.0, 1.0 };
139-
140-
// Retrieve weighted random elements
141-
var item = rand.GetItem(values, weights);
142-
var items = rand.GetItem(values, weights, 5);
143-
```
144-
145-
### Shuffling
119+
## Shuffling
146120

147121
You can shuffle the elements of an array using the `Shuffle()` method.
148122

@@ -155,22 +129,22 @@ rand.Shuffle(array);
155129

156130
This method modifies the array itself. If you need a side-effect-free shuffle, use the following LINQ extension.
157131

158-
### LINQ Extensions
132+
## LINQ Extensions
159133

160-
Under the `RandomExtensions.Linq` namespace, there are LINQ extensions that utilize random numbers.
134+
Under the `NRandom.Linq` namespace, there are LINQ extensions that utilize random numbers.
161135

162136
```cs
163137
using System;
164138
using System.Linq;
165-
using RandomExtensions.Linq;
139+
using NRandom.Linq;
166140

167141
var sequence = Enumerable.Range(0, 100);
168142

169143
// Get a random element
170144
var r = sequence.RandomElement();
171145

172-
// Shuffle the order
173-
foreach (var item in sequence.Shuffle())
146+
// Shuffle the order using NRandom
147+
foreach (var item in sequence.Shuffle(RandomEx.Shared))
174148
{
175149
Console.WriteLine(item);
176150
}
@@ -193,18 +167,17 @@ var r = sequence.RandomElement(rand);
193167

194168
## Weighted Collections
195169

196-
Under the `RandomExtensions.Collections` namespace, there are collections that hold weighted elements.
170+
Under the `NRandom.Collections` namespace, there are collections that hold weighted elements.
197171

198172
```cs
199173
// Interface for a weighted collection
200174
public interface IWeightedCollection<T> : IReadOnlyCollection<WeightedValue<T>>
201175
{
202-
T GetItem<TRandom>(TRandom random) where TRandom : IRandom;
203-
void GetItems<TRandom>(TRandom random, Span<T> destination) where TRandom : IRandom;
176+
void GetRandom<TRandom>(TRandom random, Span<T> destination) where TRandom : IRandom;
204177
}
205178

206179
// Struct representing a weighted element
207-
public readonly record struct WeightedValue<T>(T Value, double Weight);
180+
public rrecord struct WeightedValue<T>(T Value, double Weight);
208181
```
209182

210183
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.
214187
var weightedList = new WeightedList<string>();
215188

216189
// 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);
222195

223196
// Retrieve a weighted random element
224-
var rarity = weightedList.GetItem();
197+
var rarity = weightedList.GetRandom();
198+
```
199+
200+
You can also perform a weighted random draw without duplicates using `RemoveRandom()`.
201+
202+
```cs
203+
var list = new WeightedList<string>();
204+
list.Add("Foo", 1.0);
205+
list.Add("Bar", 1.5);
206+
list.Add("Baz", 3.0);
207+
208+
list.RemoveRandom(out var item0);
209+
list.RemoveRandom(out var item1);
210+
list.RemoveRandom(out var item2);
225211
```
226212

227213
## IRandom
228214

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.
230216

231217
```cs
232218
public interface IRandom
@@ -239,29 +225,36 @@ public interface IRandom
239225

240226
### IRandom Implementations
241227

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.
243229

244230
| Class Name | Algorithm |
245231
| - | - |
232+
| `ChaChaRandom` | ChaCha (default is ChaCha8) |
233+
| `MersenneTwisterRandom` | Mersenne Twister (MT19937) |
246234
| `Pcg32Random` | PCG32 (PCG-XSH-RR) |
235+
| `Philox4x32Random` | Philox4x32 (default is Philox4x32-10) |
236+
| `Sfc32Random` | SFC32 |
237+
| `Sfc64Random` | SFC64 |
247238
| `SplitMix32Random` | splitmix32 |
248239
| `SplitMix64Random` | splitmix64 |
240+
| `TinyMt32Random` | Tiny Mersenne Twister (32bit) |
241+
| `TinyMt64Random` | Tiny Mersenne Twister (64bit) |
249242
| `Xorshift32Random` | xorshift32 |
250243
| `Xorshift64Random` | xorshift64 |
251244
| `Xorshift128Random` | xorshift128 |
252245
| `Xoshiro128StarStarRandom` | xoshiro128** |
253246
| `Xoshiro256StarStarRandom` | xoshiro256** |
254247

255-
## RandomExtensions.Algorithms
248+
## NRandom.Algorithms
256249

257-
Under the `RandomExtensions.Algorithms` namespace, there are implementations of algorithms for generating pseudorandom numbers.
250+
Under the `NRandom.Algorithms` namespace, there are implementations of algorithms for generating pseudorandom numbers.
258251

259252
These are structs with minimal state and methods, useful in scenarios where performance is critical or state serialization is required.
260253

261254
Below is a sample using the `XorShift32` struct to generate pseudorandom numbers.
262255

263256
```cs
264-
using RandomExtensions.Algorithms;
257+
using NRandom.Algorithms;
265258

266259
var seed = 123456;
267260
var xorshift = new Xorshift32(seed);
@@ -271,18 +264,18 @@ var r = xorshift.Next();
271264

272265
## System.Numerics
273266

274-
`RandomEx.Numerics` package, available on NuGet, provides extensions that support types in `System.Numerics`.
267+
`NRandom.Numerics` package, available on NuGet, provides extensions that support types in `System.Numerics`.
275268

276269
#### .NET CLI
277270

278271
```ps1
279-
dotnet add package RandomEx.Numerics
272+
dotnet add package NRandom.Numerics
280273
```
281274

282275
#### Package Manager
283276

284277
```ps1
285-
Install-Package RandomEx.Numerics
278+
Install-Package NRandom.Numerics
286279
```
287280

288281
### Basic Usage
@@ -291,8 +284,8 @@ After installation, you can use the methods below.
291284

292285
```cs
293286
using System.Numerics;
294-
using RandomExtensions;
295-
using RandomExtensions.Numerics;
287+
using NRandom;
288+
using NRandom.Numerics;
296289

297290
var rand = RandomEx.Create();
298291

@@ -321,7 +314,7 @@ rand.NextQuaternionRotation(); // Get a quaternion representing a random rotati
321314

322315
## Unity
323316

324-
Random Extensions is available for use in Unity and provides an extension package.
317+
NRandom is available for use in Unity and provides an extension package.
325318

326319
### Requirements
327320

@@ -331,23 +324,23 @@ Random Extensions is available for use in Unity and provides an extension packag
331324

332325
1. Install [NugetForUnity](https://github.com/GlitchEnzo/NuGetForUnity).
333326

334-
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.
335328
![img](docs/img-nugetforunity.png)
336329

337330
3. Open the Package Manager window by selecting `Window > Package Manager`, then click on `[+] > Add package from git URL` and enter the following URL:
338331

339332
```
340-
https://github.com/AnnulusGames/RandomExtensions.git?path=src/RandomExtensions.Unity/Assets/RandomExtensions.Unity
333+
https://github.com/nuskey8/NRandom.git?path=src/NRandom.Unity/Assets/NRandom.Unity
341334
```
342335
343336
### Extension Methods
344337
345-
Under the `RandomExtensions.Unity` namespace, the following extension methods are available for Unity:
338+
Under the `NRandom.Unity` namespace, the following extension methods are available for Unity:
346339
347340
```cs
348341
using UnityEngine;
349-
using RandomExtensions;
350-
using RandomExtensions.Unity;
342+
using NRandom;
343+
using NRandom.Unity;
351344
352345
var rand = RandomEx.Create();
353346
@@ -381,6 +374,25 @@ rand.NextColorHSV(0f, 1f, 0f, 1f, 0f, 1f); // Specify HSV range
381374
rand.NextColorHSV(0f, 1f, 0f, 1f, 0f, 1f, 0f, 1f); // Specify HSV and alpha range
382375
```
383376

377+
### SerializableWeightedList
378+
379+
In addition to the regular `WeightedList<T>`, NRandom.Unity also provide `SerializableWeightedList<T>`, whose values ​​can be edited in the Inspector.
380+
381+
```cs
382+
using NRandom;
383+
using NRandom.Collections;
384+
using NRandom.Unity;
385+
using UnityEngine;
386+
387+
public class Sandbox : MonoBehaviour
388+
{
389+
[SerializeField] WeightedValue<string> value;
390+
[SerializeField] SerializableWeightedList<string> list;
391+
}
392+
```
393+
394+
![img](./docs/img-list-inspector.png)
395+
384396
## License
385397

386398
This library is released under the MIT License.

0 commit comments

Comments
 (0)