-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
rand: Reorganize random: Phase 1 #5143
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
…chanism; renaming where necessary
…r implementation later
hungrybluedev
changed the title
Reorganize random: Phase 1
rand: Reorganize random: Phase 1
Jan 30, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Phase 1 out of 3: Introduction of RNG structs
This three phase project is a reorganization of the
rand
module currently present invlib
.The first phase aims to include portable RNG implementations, impose a structure for all generators, and still allow access to the default RNG provided by
libc
viaSysRNG
.What changes does it bring?
system_rng.v
andsplitmix64.v
files contain reference implementations for RNGs.system_rng_test.v
andsplitmix64_test.v
contain uniformity, variability, reproducibility, and range checks that must pass as a bare minimum.C.rand()
function with theSysRNG
struct.rng := rand.new_default({})
with an optionalseed
parameter. (Note that{}
is a configuration struct. See the main documentation for the details on how to use short-hand syntax.)seed_data := rand.time_seed_array(1) // or however many u32s you need
rng
variable, and they want to use the functions directly, they can do so:value := rand.intn(100)
.rng
, they can equivalently do:value := rng.intn(100)
Generators added
SysRNG
- default RNG wrapping the libc implementation.SplitMix64RNG
- 64-bit version of the Split-Mix algorithmMT19937RNG
- 64-bit version of the Mersenne Prime TwisterWyRandRNG
- 64-bit RNG using thewyhash
module.PCG32RNG
- 32-bit RNG using the PCG 32 algorithmMuslRNG
- 32-bit RNG using the implementation provided in musl-libc. Intended to replacerand_r
The standard API for all RNGs in V
RNG
suffix in the name.SysRNG
,SplitMix64RNG
, etc.rng.seed(seed_data)
- whereseed_data
is a[]u32
array containing the 32bit chunks in little endian format (i.e. lower order first). It will mutate the internal state of the RNGs and cause the generated sequence to change. Reproducibility of the sequences with the corresponding seeds is guaranteed.rng.u32()
andrng.u64()
- these functions will return uniformly distributed 32-bit and 64-bit unsigned integers respectively.rng.int()
andrng.i64()
- the signed counterparts of the previous two functions; returnint
andi64
, correspondingly.rng.int31()
andrng.int63()
- will generate 31 and 63 bits of signed data asint
andi64
, respectively.rng.u32n(max)
,rng.intn(max)
,rng.i64n(max)
, andrng.u64n(max)
- return integers uniformly distributed in[0, max)
(i.e.0
inclusive andmax
exclusive).rng.{type}_in_range(min, max)
for same types as above (u32
,int
,u64
, andi64
) - return integers uniformly distributed in[min, max)
.rng.f32()
andrng.f64()
- for floating point values of the corresponding type. They will be uniformly distributed in[0.0, 1.0)
f32n(max)
,f64n(max)
,f32_in_range(min, max)
andf64_in_range(min, max)
- floating point counterparts to the integer functions.rand
package directly, except for seeding the default RNG.rng := rand.new_default({seed: seed_data})
to use the default RNG type with a custom seed.rng
can be re-seeded withrng.seed(seed_data)
later.What it does not bring? (i.e. TODO)
Remaining for phase 2
XoroshiroRNG
Remaining for phase 3
shuffle
.perm
, etc.true
with a probability ofp
in [0.0, 1.0), and other functions. (Suggestions welcome in the Discord server in the#v-development
channel)