HexMapRandom 0.1.0
dotnet add package HexMapRandom --version 0.1.0
NuGet\Install-Package HexMapRandom -Version 0.1.0
<PackageReference Include="HexMapRandom" Version="0.1.0" />
<PackageVersion Include="HexMapRandom" Version="0.1.0" />
<PackageReference Include="HexMapRandom" />
paket add HexMapRandom --version 0.1.0
#r "nuget: HexMapRandom, 0.1.0"
#:package HexMapRandom@0.1.0
#addin nuget:?package=HexMapRandom&version=0.1.0
#tool nuget:?package=HexMapRandom&version=0.1.0
hex-map-random
A lightweight, deterministic random number generator for .NET that maintains consistent sequences across serialization and deserialization cycles. Perfect for turn-based games, procedural generation, and any scenario requiring reproducible randomness.
Features
- Deterministic Sequences: Same seed always produces the same sequence of random numbers
- Serialization Support: Save and restore random generator state via JSON or binary format
- State Preservation: Maintains exact position in random sequence across save/load cycles
- Simple API: Familiar interface matching
System.Random - Lightweight: Minimal dependencies, only uses
System.Text.Json - Type-Safe: Fully documented with nullable reference types enabled
Installation
NuGet Package Manager
Install-Package HexMapRandom
.NET CLI
dotnet add package HexMapRandom
PackageReference
<PackageReference Include="HexMapRandom" Version="1.0.0" />
Usage
Basic Usage
using HexMapRandom;
// Create a new random generator with a specific seed
var random = new SerializableRandom(12345);
// Generate random numbers
int value1 = random.Next(); // Random integer
int value2 = random.Next(100); // Random integer from 0 to 99
int value3 = random.Next(50, 100); // Random integer from 50 to 99
double value4 = random.NextDouble(); // Random double from 0.0 to 1.0
JSON Serialization
// Create and use a random generator
var random = new SerializableRandom(12345);
int firstValue = random.Next(100);
int secondValue = random.Next(100);
// Serialize to JSON
string json = random.ToJson();
// Later... deserialize and continue the exact same sequence
var restored = SerializableRandom.FromJson(json);
int thirdValue = restored.Next(100); // Continues from where we left off
Binary Serialization
// Save to a stream
var random = new SerializableRandom(12345);
random.Next(100);
random.Next(100);
using (var stream = new MemoryStream())
{
random.WriteTo(stream);
// Reset stream position for reading
stream.Position = 0;
// Restore from stream
var restored = SerializableRandom.ReadFrom(stream);
int nextValue = restored.Next(100); // Continues the sequence
}
Save Game Example
public class GameState
{
public ISerializableRandom Random { get; set; }
public int Score { get; set; }
public string SaveToJson()
{
return JsonSerializer.Serialize(new
{
RandomState = Random.ToJson(),
Score
});
}
public static GameState LoadFromJson(string json)
{
var data = JsonSerializer.Deserialize<JsonElement>(json);
return new GameState
{
Random = SerializableRandom.FromJson(data.GetProperty("RandomState").GetString()!),
Score = data.GetProperty("Score").GetInt32()
};
}
}
How It Works
The SerializableRandom class wraps .NET's standard Random class and tracks two pieces of state:
- Seed: The initial value used to create the random sequence
- Call Count: The number of times random generation methods have been called
When deserializing, the generator:
- Creates a new
Randominstance with the saved seed - Replays the exact number of calls to restore the sequence position
- Resumes generating numbers from the correct point in the sequence
This ensures that after deserialization, the random generator produces the exact same sequence as if it had never been serialized.
Thread Safety
?? WARNING: SerializableRandom is NOT thread-safe. If you need to access the same instance from multiple threads, you must provide external synchronization (e.g., using lock statements).
This library is designed for single-threaded, turn-based scenarios where deterministic randomness is required.
Requirements
- .NET 10.0 or later
- System.Text.Json (included in .NET)
License
This project is licensed under the GNU Lesser General Public License v3.0 (LGPL-3.0) - see the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit issues or pull requests.
Repository
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.1.0 | 259 | 11/30/2025 |