ociaw / randn Goto Github PK
View Code? Open in Web Editor NEWBetter random number generation for .NET
Home Page: https://randn.dev
License: MIT License
Better random number generation for .NET
Home Page: https://randn.dev
License: MIT License
Currently we just have UniformDouble
, which needs more unit tests for thorough testing.
In addition, we want specialized distributions for the interval 0-1.
We'll need three implementations for each distribution, Single
, Double
, and Decimal
. Extensive tests are needed, including value-stability testing.
The IDistribution<T>
interface forms a monad. Is there scope for adding implementations of unit, map and bind to RandN? Or perhaps to add Select
and SelectMany
extension methods so that it could be used with query syntax.
Since seeds are able to be reused at any time, they need to be serializable so they can be persisted to the file system, sent across a network, etc.
Right now we rely on C# 8's nullable reference types to keep nulls out. This isn't perfect, and doesn't work at all if the consumer doesn't have them enabled as well. Instead, we can use NullGuard to add null checks on public methods automatically.
These types have been introduced in .NET 7 and need implementations.
We have ociaw.com/randn, but the website should be independent, and in the RandN repository.
We need to generate documentation from the doc comments, as well as use the existing documentation.
We need to add wrapper RNGs with easy to understand names, so people don't have to trawl through the Rngs namespace and figure out the difference between ChaCha and Mersenne Twister. None of these need to be reproducible, since in that case the user will have to figure out which algorithm they want anyway.
Rand provides 4 different RNGs,
We'll treat CryptoServiceProvider as similar enough OsRng, so there's no need to reimplement it. ChaCha is a good fit for StdRng, just need to decide how many rounds. ThreadLocal can be used to create an equivalent of ThreadRng through StdRng. PCG32 is a good candidate for SmallRng, though some statistical quality issues have been found recently.
Type names are TBD - SmallRng/FastRng/InsecureRng, StdRng/StandardRng/Rng, ThreadRng/ThreadLocalRng?
https://devblogs.microsoft.com/dotnet/announcing-net-6/
The new BitConverter methods could be useful, and new Vector APIs could make it feasible to implement vectorized ChaCha in a instruction set agnostic manner.
Distributions are rather messy at the moment - they're swamped by various implementations of the Uniform distribution. This namespace should be cleaned up - possibilities:
Uniform
RandN.Distributions.Uniforms
Uniform
through IDistribution
RNGCryptoServiceProvider is obsolete as of .NET 6, hence we need to move to RandomNumberGenerator.Create()
instead. The issue is naming - perhaps SystemCryptoRng
would work?
Investigate ensuring that the built NuGet packages are reproducible.
The docsite fails to build on the build server, but not locally on my computer.
https://builds.sr.ht/~ociaw/job/316214
Executing pipeline "Resources" (20/24) with 1 child module(s)
Error while copying file /home/build/RandN/docs/input/assets/css/override.css: The process cannot access the file '/home/build/RandN/docs/output/assets/css/override.css' because it is being used by another process.
Error while executing module CopyFiles
Error while executing pipeline Resources
Exception during execution: System.AggregateException: One or more errors occurred. (One or more errors occurred. (The process cannot access the file '/home/build/RandN/docs/output/assets/css/override.css' because it is being used by another process.)) (The process cannot access the file '/home/build/RandN/docs/output/assets/css/override.css' because it is being used by another process.) ---> System.IO.IOException: The process cannot access the file '/home/build/RandN/docs/output/assets/css/override.css' because it is being used by another process.
at System.IO.FileStream.Init(FileMode mode, FileShare share)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
at System.IO.FileSystem.CopyFile(String sourceFullPath, String destFullPath, Boolean overwrite)
at System.IO.FileInfo.CopyTo(String destFileName, Boolean overwrite)
at Wyam.Core.IO.FileProviders.Local.RetryHelper.Retry[T](Func`1 func) in C:\Code\Wyam\Wyam\src\core\Wyam.Core\IO\FileProviders\Local\RetryHelper.cs:line 22
at Wyam.Core.IO.FileProviders.Local.LocalFile.CopyTo(IFile destination, Boolean overwrite, Boolean createDirectory) in C:\Code\Wyam\Wyam\src\core\Wyam.Core\IO\FileProviders\Local\LocalFile.cs:line 67
at Wyam.Core.Modules.IO.CopyFiles.<>c__DisplayClass10_0.<Execute>b__1(IFile file) in C:\Code\Wyam\Wyam\src\core\Wyam.Core\Modules\IO\CopyFiles.cs:line 147
at System.Linq.Parallel.SelectQueryOperator`2.SelectQueryOperatorEnumerator`1.MoveNext(TOutput& currentElement, TKey& currentKey)
at System.Linq.Parallel.WhereQueryOperator`1.WhereQueryOperatorEnumerator`1.MoveNext(TInputOutput& currentElement, TKey& currentKey)
at System.Linq.Parallel.PipelineSpoolingTask`2.SpoolingWork()
at System.Linq.Parallel.SpoolingTaskBase.Work()
at System.Linq.Parallel.QueryTask.BaseWork(Object unused)
at System.Linq.Parallel.QueryTask.<>c.<.cctor>b__10_0(Object o)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
C# and .NET do not guarantee the implementation of floating point operations - this causes the distribution to sample different numbers on different platforms. For example, on x86, the .NET Framework uses the 80-bit x87 FPU, but uses 64-bit SSE2 instructions on x64.
Where possible (.NET Core 3.1+), we will explicitly use SSE2 instructions. Otherwise, we will do the following: On application start, perform floating point tests to determine if the underlying hardware uses 64-bit or 80-bit floating point operations. If the former, we can use those directly. If the latter, we fallback to a software implementation (based on SoftFloat).
We also want to keep current floating point implementations, but move them into a separate package (such as "UnstableDists" or similar). This lets users use the faster non-software implementation if they can guarantee the platform they are running on or don't care about reproducibility.
XorShift has no tests to verify it works as expected.
Pcg32 can likely be easily vectorized for an increase in throughput. It may be desired to keep a non-vectorized implementation available, however, since the vectorized implementation may use more memory for buffering.
3 floating point distribution tests fail when run on specifically x86 .NET Framework. I believe this is caused by x86 using extended (80-bit) precision FP math, while x64 and .NET Core uses standard (64-bit) precision through SSE2.
As correcting this will change the behavior of a distribution, it can be included in 0.2 at the earliest.
Hi,
I get the following exception from time to time, now sure what is the root cause.
this is how i intantiate my _rng instance:
var rng = ChaCha.GetChaCha8Factory().Create(ThreadLocalRng.Instance);
I call the following method:
rng.NextUInt32()
Stack trace:
Exception has occurred: CLR/System.IndexOutOfRangeException
Exception thrown: 'System.IndexOutOfRangeException' in RandN.Core.dll: 'Index was outside the bounds of the array.'
at RandN.Implementation.BlockBuffer32`2.NextUInt32()
at RandN.Rngs.ChaCha.NextUInt32()
XorShift and MersenneTwister should be moved to separate packages since they aren't an essential part of RandN.
ChaCha contains a significant amount of platform specific code for SIMD support, and is better suited in a separate project to isolate tests.
The above also applies to Pcg32, as it may be vectorized in the future as well.
The Rand package will end up depending on Rand.ChaCha and Rand.Pcg32.
https://devblogs.microsoft.com/dotnet/welcome-to-csharp-10/
File-scoped namespaces in particular will save precious whitespace.
Dependent on whether the desired features will continue to work in .NET Framework 4.8.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.