13 Commits
Author SHA1 Message Date
tsommer d8f024a5c5 Merge branch '8-bug-indexoutofrangeexception-when-using-multi-threaded-consumers' into 'main'
Resolve "Bug: IndexOutOfRangeException when using multi-threaded consumers"

Closes #8

See merge request open-source/dotnet/FastRng!6
2023-11-14 14:43:58 +00:00
Thorsten Sommer f1fe1de090 Increment version 2023-11-14 15:43:11 +01:00
Thorsten Sommer a838dacf2a Fixed rare index bug when using multiple multithreading consumers 2023-11-14 15:38:41 +01:00
Thorsten Sommer faf6c99c19 Added unit test to cover issue #8 2023-11-14 13:57:37 +01:00
tsommer 9024752b53 Merge branch '6-preparing-v1-1-1' into 'main'
Resolve "Preparing v1.1.1"

Closes #6

See merge request open-source/dotnet/FastRng!5
2023-10-26 07:28:56 +00:00
Thorsten Sommer e932df93cf Increased version number 2023-10-26 09:27:42 +02:00
tsommer 2e9a5e336f Merge branch '5-bug-cannot-implement-own-distributions' into 'main'
Resolve "Bug: Cannot implement own distributions"

Closes #5

See merge request open-source/dotnet/FastRng!4
2023-10-25 13:22:04 +00:00
Thorsten Sommer e98378f246 Refactored ShapeFunction 2023-10-25 15:21:16 +02:00
tsommer fc8e666680 Merge branch '5-bug-cannot-implement-own-distributions' into 'main'
Resolve "Bug: Cannot implement own distributions"

Closes #5

See merge request open-source/dotnet/FastRng!3
2023-10-25 13:09:23 +00:00
Thorsten Sommer 3b62152957 Allow users to implement their own distributions 2023-10-25 15:07:42 +02:00
tsommer 7d56951040 Merge branch '3-include-readme-in-nuget-package' into 'main'
Resolve "Include readme in NuGet package"

Closes #3

See merge request open-source/dotnet/FastRng!2
2023-07-10 18:00:54 +00:00
tsommer 2f836892d1 Include readme for NuGet releases 2023-07-10 20:00:08 +02:00
tsommer 146d524da8 Merge branch '1-migrate-to-inumber' into 'main'
Resolve "Migrate to INumber"

Closes #1 and #2

See merge request open-source/dotnet/FastRng!1
2023-07-10 17:42:08 +00:00
24 changed files with 82 additions and 27 deletions

No files matched your search

+2 -1
View File
@@ -12,5 +12,6 @@ public sealed class BetaA2B2<TNum> : Distribution<TNum> where TNum : IFloatingPo
{ {
} }
private protected override TNum ShapeFunction(TNum x) => CONSTANT * TNum.Pow(x, ALPHA - TNum.One) * TNum.Pow(TNum.One - x, BETA - TNum.One); /// <inheritdoc />
public override TNum ShapeFunction(TNum x) => CONSTANT * TNum.Pow(x, ALPHA - TNum.One) * TNum.Pow(TNum.One - x, BETA - TNum.One);
} }
+2 -1
View File
@@ -12,5 +12,6 @@ public sealed class BetaA2B5<TNum> : Distribution<TNum> where TNum : IFloatingPo
{ {
} }
private protected override TNum ShapeFunction(TNum x) => CONSTANT * TNum.Pow(x, ALPHA - TNum.One) * TNum.Pow(TNum.One - x, BETA - TNum.One); /// <inheritdoc />
public override TNum ShapeFunction(TNum x) => CONSTANT * TNum.Pow(x, ALPHA - TNum.One) * TNum.Pow(TNum.One - x, BETA - TNum.One);
} }
+3 -2
View File
@@ -11,6 +11,7 @@ public sealed class BetaA5B2<TNum> : Distribution<TNum> where TNum : IFloatingPo
public BetaA5B2(IRandom<TNum> rng) : base(rng) public BetaA5B2(IRandom<TNum> rng) : base(rng)
{ {
} }
private protected override TNum ShapeFunction(TNum x) => CONSTANT * TNum.Pow(x, ALPHA - TNum.One) * TNum.Pow(TNum.One - x, BETA - TNum.One); /// <inheritdoc />
public override TNum ShapeFunction(TNum x) => CONSTANT * TNum.Pow(x, ALPHA - TNum.One) * TNum.Pow(TNum.One - x, BETA - TNum.One);
} }
+2 -1
View File
@@ -13,5 +13,6 @@ public sealed class CauchyLorentzX0<TNum> : Distribution<TNum> where TNum : IFlo
{ {
} }
private protected override TNum ShapeFunction(TNum x) => CONSTANT * ( TNum.One / (TNum.Pi * SCALE)) * ((SCALE * SCALE) / (TNum.Pow(x - MEDIAN, TWO) + (SCALE * SCALE))); /// <inheritdoc />
public override TNum ShapeFunction(TNum x) => CONSTANT * ( TNum.One / (TNum.Pi * SCALE)) * ((SCALE * SCALE) / (TNum.Pow(x - MEDIAN, TWO) + (SCALE * SCALE)));
} }
+2 -1
View File
@@ -13,5 +13,6 @@ public sealed class CauchyLorentzX1<TNum> : Distribution<TNum> where TNum : IFlo
{ {
} }
private protected override TNum ShapeFunction(TNum x) => CONSTANT * (TNum.One / (TNum.Pi * SCALE)) * ((SCALE * SCALE) / (TNum.Pow(x - MEDIAN, TWO) + (SCALE * SCALE))); /// <inheritdoc />
public override TNum ShapeFunction(TNum x) => CONSTANT * (TNum.One / (TNum.Pi * SCALE)) * ((SCALE * SCALE) / (TNum.Pow(x - MEDIAN, TWO) + (SCALE * SCALE)));
} }
+2 -1
View File
@@ -24,5 +24,6 @@ public sealed class ChiSquareK1<TNum> : Distribution<TNum> where TNum : IFloatin
{ {
} }
private protected override TNum ShapeFunction(TNum x) => CONSTANT * ((TNum.Pow(x, K_HALF_MINUS_ONE) * TNum.Exp(-x * HALF)) / DIVISOR); /// <inheritdoc />
public override TNum ShapeFunction(TNum x) => CONSTANT * ((TNum.Pow(x, K_HALF_MINUS_ONE) * TNum.Exp(-x * HALF)) / DIVISOR);
} }
+2 -1
View File
@@ -24,5 +24,6 @@ public sealed class ChiSquareK10<TNum> : Distribution<TNum> where TNum : IFloati
{ {
} }
private protected override TNum ShapeFunction(TNum x) => CONSTANT * ((TNum.Pow(x, K_HALF_MINUS_ONE) * TNum.Exp(-x * HALF)) / DIVISOR); /// <inheritdoc />
public override TNum ShapeFunction(TNum x) => CONSTANT * ((TNum.Pow(x, K_HALF_MINUS_ONE) * TNum.Exp(-x * HALF)) / DIVISOR);
} }
+2 -1
View File
@@ -24,5 +24,6 @@ public sealed class ChiSquareK4<TNum> : Distribution<TNum> where TNum : IFloatin
{ {
} }
private protected override TNum ShapeFunction(TNum x) => CONSTANT * ((TNum.Pow(x, K_HALF_MINUS_ONE) * TNum.Exp(-x * HALF)) / DIVISOR); /// <inheritdoc />
public override TNum ShapeFunction(TNum x) => CONSTANT * ((TNum.Pow(x, K_HALF_MINUS_ONE) * TNum.Exp(-x * HALF)) / DIVISOR);
} }
+1 -1
View File
@@ -16,7 +16,7 @@ public abstract class Distribution<TNum> : IDistribution<TNum> where TNum : IFlo
this.fitter = new ShapeFitter<TNum>(this.ShapeFunction, rng, 100); this.fitter = new ShapeFitter<TNum>(this.ShapeFunction, rng, 100);
} }
private protected abstract TNum ShapeFunction(TNum x); public abstract TNum ShapeFunction(TNum x);
public TNum GetDistributedValue(CancellationToken token = default) => this.fitter.NextNumber(token); public TNum GetDistributedValue(CancellationToken token = default) => this.fitter.NextNumber(token);
+2 -1
View File
@@ -11,5 +11,6 @@ public sealed class ExponentialLa10<TNum> : Distribution<TNum> where TNum : IFlo
{ {
} }
private protected override TNum ShapeFunction(TNum x) => CONSTANT * LAMBDA * TNum.Exp(-LAMBDA * x); /// <inheritdoc />
public override TNum ShapeFunction(TNum x) => CONSTANT * LAMBDA * TNum.Exp(-LAMBDA * x);
} }
+2 -1
View File
@@ -11,5 +11,6 @@ public sealed class ExponentialLa5<TNum> : Distribution<TNum> where TNum : IFloa
{ {
} }
private protected override TNum ShapeFunction(TNum x) => CONSTANT * LAMBDA * TNum.Exp(-LAMBDA * x); /// <inheritdoc />
public override TNum ShapeFunction(TNum x) => CONSTANT * LAMBDA * TNum.Exp(-LAMBDA * x);
} }
+2 -1
View File
@@ -21,5 +21,6 @@ public sealed class GammaA5B15<TNum> : Distribution<TNum> where TNum : IFloating
{ {
} }
private protected override TNum ShapeFunction(TNum x) => CONSTANT * ((BETA_TO_THE_ALPHA * TNum.Pow(x, ALPHA - TNum.One) * TNum.Exp(-BETA * x)) / GAMMA_ALPHA); /// <inheritdoc />
public override TNum ShapeFunction(TNum x) => CONSTANT * ((BETA_TO_THE_ALPHA * TNum.Pow(x, ALPHA - TNum.One) * TNum.Exp(-BETA * x)) / GAMMA_ALPHA);
} }
@@ -11,5 +11,6 @@ public sealed class InverseExponentialLa10<TNum> : Distribution<TNum> where TNum
{ {
} }
private protected override TNum ShapeFunction(TNum x) => CONSTANT * LAMBDA * TNum.Exp(LAMBDA * x); /// <inheritdoc />
public override TNum ShapeFunction(TNum x) => CONSTANT * LAMBDA * TNum.Exp(LAMBDA * x);
} }
@@ -11,5 +11,6 @@ public sealed class InverseExponentialLa5<TNum> : Distribution<TNum> where TNum
{ {
} }
private protected override TNum ShapeFunction(TNum x) => CONSTANT * LAMBDA * TNum.Exp(LAMBDA * x); /// <inheritdoc />
public override TNum ShapeFunction(TNum x) => CONSTANT * LAMBDA * TNum.Exp(LAMBDA * x);
} }
+2 -1
View File
@@ -22,5 +22,6 @@ public sealed class InverseGammaA3B05<TNum> : Distribution<TNum> where TNum : IF
{ {
} }
private protected override TNum ShapeFunction(TNum x) => FACTOR_LEFT * TNum.Pow(x, -ALPHA - TNum.One) * TNum.Exp(-BETA / x); /// <inheritdoc />
public override TNum ShapeFunction(TNum x) => FACTOR_LEFT * TNum.Pow(x, -ALPHA - TNum.One) * TNum.Exp(-BETA / x);
} }
+2 -1
View File
@@ -20,5 +20,6 @@ public sealed class LaplaceB01M0<TNum> : Distribution<TNum> where TNum : IFloati
{ {
} }
private protected override TNum ShapeFunction(TNum x) => FACTOR_LEFT * TNum.Exp(-TNum.Abs(x - MU) / B); /// <inheritdoc />
public override TNum ShapeFunction(TNum x) => FACTOR_LEFT * TNum.Exp(-TNum.Abs(x - MU) / B);
} }
+2 -1
View File
@@ -20,5 +20,6 @@ public sealed class LaplaceB01M05<TNum> : Distribution<TNum> where TNum : IFloat
{ {
} }
private protected override TNum ShapeFunction(TNum x) => FACTOR_LEFT * TNum.Exp(-TNum.Abs(x - MU) / B); /// <inheritdoc />
public override TNum ShapeFunction(TNum x) => FACTOR_LEFT * TNum.Exp(-TNum.Abs(x - MU) / B);
} }
+2 -1
View File
@@ -20,5 +20,6 @@ public sealed class LogNormalS1M0<TNum> : Distribution<TNum> where TNum : IFloat
{ {
} }
private protected override TNum ShapeFunction(TNum x) => (CONSTANT / (x * FACTOR)) * TNum.Exp( -(TNum.Pow(TNum.Log(x) - MU, TWO) / (TWO * TNum.Pow(SIGMA, TWO)))); /// <inheritdoc />
public override TNum ShapeFunction(TNum x) => (CONSTANT / (x * FACTOR)) * TNum.Exp( -(TNum.Pow(TNum.Log(x) - MU, TWO) / (TWO * TNum.Pow(SIGMA, TWO))));
} }
+2 -1
View File
@@ -14,5 +14,6 @@ public sealed class NormalS02M05<TNum> : Distribution<TNum> where TNum : IFloati
{ {
} }
private protected override TNum ShapeFunction(TNum x) => TNum.One / (STD_DEV * SQRT_2_PI) * TNum.Exp(NEGATIVE_HALF * TNum.Pow((x - MEAN) / STD_DEV, TWO)); /// <inheritdoc />
public override TNum ShapeFunction(TNum x) => TNum.One / (STD_DEV * SQRT_2_PI) * TNum.Exp(NEGATIVE_HALF * TNum.Pow((x - MEAN) / STD_DEV, TWO));
} }
+2 -1
View File
@@ -26,5 +26,6 @@ public sealed class StudentTNu1<TNum> : Distribution<TNum> where TNum : IFloatin
{ {
} }
private protected override TNum ShapeFunction(TNum x) => CONSTANT * TNum.Pow((DIVIDEND / DIVISOR) * TNum.Pow( TNum.One + TNum.Pow(START + x * COMPRESS, TWO) / NU, EXPONENT), COMPRESS); /// <inheritdoc />
public override TNum ShapeFunction(TNum x) => CONSTANT * TNum.Pow((DIVIDEND / DIVISOR) * TNum.Pow( TNum.One + TNum.Pow(START + x * COMPRESS, TWO) / NU, EXPONENT), COMPRESS);
} }
+2 -1
View File
@@ -12,5 +12,6 @@ public sealed class WeibullK05La1<TNum> : Distribution<TNum> where TNum : IFloat
{ {
} }
private protected override TNum ShapeFunction(TNum x) => CONSTANT * ( (K / LAMBDA) * TNum.Pow(x / LAMBDA, K - TNum.One) * TNum.Exp(-TNum.Pow(x/LAMBDA, K))); /// <inheritdoc />
public override TNum ShapeFunction(TNum x) => CONSTANT * ( (K / LAMBDA) * TNum.Pow(x / LAMBDA, K - TNum.One) * TNum.Exp(-TNum.Pow(x/LAMBDA, K)));
} }
+7 -4
View File
@@ -3,14 +3,15 @@
<PropertyGroup> <PropertyGroup>
<TargetFramework>net7.0</TargetFramework> <TargetFramework>net7.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<AssemblyVersion>1.1.0</AssemblyVersion> <AssemblyVersion>1.1.2</AssemblyVersion>
<FileVersion>1.1.0</FileVersion> <FileVersion>1.1.2</FileVersion>
<PackageVersion>1.1.0</PackageVersion> <PackageVersion>1.1.2</PackageVersion>
<Authors>Thorsten Sommer</Authors> <Authors>Thorsten Sommer</Authors>
<PackageProjectUrl>https://devops.tsommer.org/open-source/dotnet/FastRng</PackageProjectUrl> <PackageProjectUrl>https://devops.tsommer.org/open-source/dotnet/FastRng</PackageProjectUrl>
<RepositoryUrl>https://devops.tsommer.org/open-source/dotnet/FastRng</RepositoryUrl> <RepositoryUrl>https://devops.tsommer.org/open-source/dotnet/FastRng</RepositoryUrl>
<PackageLicenseExpression>BSD-3-Clause</PackageLicenseExpression> <PackageLicenseExpression>BSD-3-Clause</PackageLicenseExpression>
<LangVersion>latest</LangVersion> <LangVersion>latest</LangVersion>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' "> <PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
@@ -21,5 +22,7 @@
<DocumentationFile>bin\Release\FastRng.xml</DocumentationFile> <DocumentationFile>bin\Release\FastRng.xml</DocumentationFile>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<None Include="..\README.md" Pack="true" PackagePath="\"/>
</ItemGroup>
</Project> </Project>
+6 -1
View File
@@ -295,6 +295,11 @@ public sealed class MultiThreadedRng<TNum> : IRandom<TNum>, IDisposable where TN
// Made a local copy of the current pointer: // Made a local copy of the current pointer:
var myPointer = this.currentBufferPointer; var myPointer = this.currentBufferPointer;
// Issue #8: This might happens when the current thread was interrupted by another thread, and
// the other thread has already updated the pointer. In this case, we start over again.
if (myPointer >= BUFFER_SIZE)
goto Start;
// Increment the pointer for the next thread or call: // Increment the pointer for the next thread or call:
var nextPointer = myPointer + 1; var nextPointer = myPointer + 1;
@@ -303,7 +308,7 @@ public sealed class MultiThreadedRng<TNum> : IRandom<TNum>, IDisposable where TN
goto Start; goto Start;
// //
// Case: Success. We updated the pointer and, thus, can use the read number. // Case: Success. We updated the pointer and, thus, can use it to read the number.
// //
return this.currentBuffer[myPointer]; return this.currentBuffer[myPointer];
} }
+27
View File
@@ -1,6 +1,7 @@
using System; using System;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Threading; using System.Threading;
using System.Threading.Tasks;
using FastRng; using FastRng;
using FastRng.Distributions; using FastRng.Distributions;
using NUnit.Framework; using NUnit.Framework;
@@ -222,6 +223,32 @@ public class MultiThreadedRngTests
Assert.That(lorentzContains0, Is.False, "Lorentz distribution contained 0"); Assert.That(lorentzContains0, Is.False, "Lorentz distribution contained 0");
Assert.That(lorentzContains1, Is.True, "Lorentz distribution does not contained 1"); Assert.That(lorentzContains1, Is.True, "Lorentz distribution does not contained 1");
} }
[Test]
[Category(TestCategories.LONG_RUNNING)]
public void TestMultiThreadedConsumer()
{
var job1 = Task.Factory.StartNew(Run, TaskCreationOptions.LongRunning);
var job2 = Task.Factory.StartNew(Run, TaskCreationOptions.LongRunning);
var job3 = Task.Factory.StartNew(Run, TaskCreationOptions.LongRunning);
Assert.DoesNotThrowAsync(async () => await job1);
Assert.DoesNotThrowAsync(async () => await job2);
Assert.DoesNotThrowAsync(async () => await job3);
return;
float Run()
{
var sum = 0f;
var distLorentz = new CauchyLorentzX1<float>(this.rng);
for (int i = 0; i < 100_000_000; i++)
{
sum += distLorentz.NextNumber();
}
return sum;
}
}
[Test] [Test]
[Category(TestCategories.COVER)] [Category(TestCategories.COVER)]