https://github.com/depra-inc/random
Service containing a random value generator
https://github.com/depra-inc/random
cryptorandom csharp pseudorandom random random-generation random-number-generators randomization randomizer saferandom service threadsafety
Last synced: 3 months ago
JSON representation
Service containing a random value generator
- Host: GitHub
- URL: https://github.com/depra-inc/random
- Owner: Depra-Inc
- License: apache-2.0
- Created: 2022-10-07T19:38:17.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-10-19T12:51:19.000Z (over 3 years ago)
- Last Synced: 2025-08-22T05:24:50.235Z (5 months ago)
- Topics: cryptorandom, csharp, pseudorandom, random, random-generation, random-number-generators, randomization, randomizer, saferandom, service, threadsafety
- Language: C#
- Homepage:
- Size: 192 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Random Service
Contains a Random Service that provides an **IRandomizer** abstraction whose contract is equivalent to **System.Random**.
## Features
- **New** random number generators based on **System.Random**:
- **ConcurrentPseudoRandom** - A random number generator guaranteeing thread safety;
- **CryptoRandom** - A random number generator based on the System.Security.Cryptography.RNGCryptoServiceProvider.
- Supported **types**:
1. Int32/Int
2. Double
3. Byte[]
- Supported **types** via **extension** methods:
1. SByte
2. Byte
3. Int16/Short
4. UInt16/UShort
5. UInt32/UInt
6. Int64/Long
7. UInt64/ULong
8. Float
9. Decimal
10. Char[]
11. String
12. Boolean
13. Enum
- Extensibility (see **[Depra.Unity.Random](https://github.com/Depression-aggression/Unity-Random)**);
- Most of the functionality is covered by **unit tests;**
- Also included are **benchmarks** that may be of interest.
## Usage
To instantiate a service you need to use the **builder** pattern.
Instance creation with a **custom randomizer**:
```csharp
var randomService = new RandomServiceBuilder()
.With(new CutomIntRandomizer()) // Or another randomizer
.Build(); // for type Int32
```
Instantiating with a **collection** of **randomizers**:
```csharp
var randomService = new RandomServiceBuilder()
.With(new PseudoRandomizers()) // Or another collection of randomizers.
.Build();
// You can get randomizers from collections working with System.Random in this way:
var intRandomizer = randomService.GetRandomizer(typeof(int))
intRandomizer = randomService.GetNumberRandomizer();
var doubleRandomizer = randomService.GetTypedRandomizer();
var byteArrayRandomizer = randomService.GetArrayRandomizer();
```
With the help of **extension methods** for randomizers, you can also get random value types that are not supported through System.Random.
An example of getting a **random string** using **INumberRandomizer**:
```csharp
var intRandomizer = randomService.GetNumberRandomizer();
var randUpperCaseString = intRandomizer.NextString(length: 10, includeLowerCase: false);
var randString = intRandomizer.NextString(length: 20, allowedCharacters: "abcdef");
```
## Integrations:
- **[Depra.Unity.Random](https://github.com/Depression-aggression/Unity-Random)** - To provide support for UnityEngine.Random.
## Ps
Some extension methods may not perform well and will be improved in future releases.
Your suggestions for improvements are welcome.