From 119f2e9dd1cefe43ea13ae04c4068590b606a249 Mon Sep 17 00:00:00 2001 From: godzzz Date: Wed, 19 Oct 2022 15:49:19 +0300 Subject: [PATCH] Random project & Readme updated --- README.md | 90 ++++++++++++++++++++++++++++---------------- Random/Random.csproj | 7 ++-- 2 files changed, 62 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index 66f7571..7de48ff 100644 --- a/README.md +++ b/README.md @@ -2,49 +2,75 @@ Contains a Random Service that provides an **IRandomizer** abstraction whose contract is equivalent to **System.Random**. -Most of the functionality is covered by unit tests. +## 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. -Also included are benchmarks that may be of interest. - -### List of available randomizers: - -- **PseudoRandom** - Decorator for System.Random, to support the IRandomizer contract; -- **ConcurrentPseudoRandom** - A random number generator guaranteeing thread safety; -- **CryptoRandom** - A random number generator based on the System.Security.Cryptography.RNGCryptoServiceProvider. +## Usage -### Supported types: +To instantiate a service you need to use the **builder** pattern. -1. Int32/Int -2. Double -3. Byte[] +Instance creation with a **custom randomizer**: -### Supported types via extension methods: +```csharp +var randomService = new RandomServiceBuilder() + .With(new CutomIntRandomizer()) // Or another randomizer + .Build(); // for type Int32 +``` -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 +Instantiating with a **collection** of **randomizers**: -## Usage +```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(); +``` -A service is best consumed through an IoC container. But nothing prevents you from creating an instance where you see fit. +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 randomizer = new PseudoRandom(); -var randomService = new RandomService(randomizer); - -var randomValue = randomService.GetRandomizer().NextDouble(0, 100); +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. diff --git a/Random/Random.csproj b/Random/Random.csproj index 6b83ab5..4625016 100644 --- a/Random/Random.csproj +++ b/Random/Random.csproj @@ -4,9 +4,9 @@ Depra.Random Depra.Random netstandard2.0 - 1.1.0.0 - 1.1.0.0 - 1.1.0 + 1.1.1.0 + 1.1.1.0 + 1.1.1 true true Nikolay Melnikov @@ -17,6 +17,7 @@ https://github.com/Depression-aggression/RandomService Module csharp, service, random, randomization, random-generation, random-number-generators, randomizer, pseudorandom, threadsafety, cryptorandom, saferandom + 1.1.1