Skip to content

Commit

Permalink
Random project & Readme updated
Browse files Browse the repository at this point in the history
  • Loading branch information
g0dzZz-coder committed Oct 19, 2022
1 parent bfbd8c0 commit 119f2e9
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 35 deletions.
90 changes: 58 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(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<int>();
var doubleRandomizer = randomService.GetTypedRandomizer<double>();
var byteArrayRandomizer = randomService.GetArrayRandomizer<byte[]>();
```

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<int>**:

```csharp
var randomizer = new PseudoRandom();
var randomService = new RandomService(randomizer);

var randomValue = randomService.GetRandomizer().NextDouble(0, 100);
var intRandomizer = randomService.GetNumberRandomizer<int>();
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.
Expand Down
7 changes: 4 additions & 3 deletions Random/Random.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
<AssemblyName>Depra.Random</AssemblyName>
<RootNamespace>Depra.Random</RootNamespace>
<TargetFramework>netstandard2.0</TargetFramework>
<AssemblyVersion>1.1.0.0</AssemblyVersion>
<FileVersion>1.1.0.0</FileVersion>
<Version>1.1.0</Version>
<AssemblyVersion>1.1.1.0</AssemblyVersion>
<FileVersion>1.1.1.0</FileVersion>
<Version>1.1.1</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<Authors>Nikolay Melnikov</Authors>
Expand All @@ -17,6 +17,7 @@
<RepositoryUrl>https://github.com/Depression-aggression/RandomService</RepositoryUrl>
<RepositoryType>Module</RepositoryType>
<PackageTags>csharp, service, random, randomization, random-generation, random-number-generators, randomizer, pseudorandom, threadsafety, cryptorandom, saferandom</PackageTags>
<PackageVersion>1.1.1</PackageVersion>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 119f2e9

Please sign in to comment.