feat(examples): Add a useful set of high quality pseudo-random number generators #2868
+2,336
−28
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I ported a number of my pseudo-random number generator implementations from Ruby to gno while traveling to the retreat last weekend as an exercise in expanding my comfort level with gno code while contributing code that others may find interesting or useful.
I added two xorshift generators, xorshift64* and xorshiftr128+. These are both many times faster than the PCG generator that is the gno default, and produce high quality randomness with great statistical qualities. In addition to these, I added both the 32-bit ISAAC implementation (with an added function to return 64 bit values), and the 64-bit ISAAC implementation. ISAAC is a stellar pseudo-random number generator. Both implementations are significantly faster than PCG (though not near so fast as the xorshift algorithms), while producing extremely high quality, cryptographically secure randomness that can not be differentiated from real randomness.
All of these were built to be compatible with the standard Rand() implementation. This means that any of these can be used as a drop-in replacement for the default PCG algorithm:
All of these leverage the
gno.land/p/demo/entropy
package to assist with seeding if no seed is provided. In the case of the ISAAC algorithms, they require 256 uint values for their seed, so they leverage a combination ofentropy
andxorshiftr128+
to generate any missing numbers in the provided seed.I also added a function to entropy to return uint64, to facilitate using it for seeding.
I added tests to entropy, and wrote tests for the other generators, as well.
There are a few other things that ended up in this PR. In order to make some fact based assertions about the performance of these generators, I included some code that can be ran via
gno run -expr
. i.e.gno run -expr 'averageISAAC()' isaac.gno
that can be used to get some benchmarks and some very simple self-statistical-analysis on the results, and when I did so, I discovered that the currentufmt.Sprintf
implementation didn't support%f
output. So, I added that to it's capabilities, which, in turn, required addingFormatFloat
to thestrconv.gno/strconv.go
implementation in the standard library. I added a test to cover this.I also noticed that there is a test in
tm2/pkg/p2p
that is failing on both master and my branch, so I took what may be a naive approach to fixing it.Contributors' checklist...
BREAKING CHANGE: xxx
message was included in the description