Skip to content
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 2 #5289

Merged
merged 27 commits into from
Jun 9, 2020
Merged

rand: Reorganize random: Phase 2 #5289

merged 27 commits into from
Jun 9, 2020

Conversation

hungrybluedev
Copy link
Member

@hungrybluedev hungrybluedev commented Jun 8, 2020

Phase 2 out of 3: Adding global functions and clean-up

This three phase project is a reorganization of the rand module currently present in vlib.

The second phase is a clean-up of the rand module. It aims to bring global, top-level functions to reduce the friction for usage for casual users. The ability to choose a generator remains mostly intact. The only change is that the generators will move to their respective modules.

What changes does it bring?

  • Support for a "global" RNG and top-level functions that use it.
  • Move util.v to the util module. It contains frequently used constants and helper functions to create time-based seed arrays and values.
  • Move all generators to their respective modules.
  • Remove legacy, deprecated functions such as rand_f32(max), rand_f64(max), etc. that depend on C.RAND_MAX.
  • Remove all deprecated usages and update them to use the new versions of the random library.
  • Write good documentation for the module: including tutorials, caveats, etc.

What it does not bring? (i.e. TODO)

Remaining for phase 3

  1. Add functions for non-uniform distributions like normal, exponential, beta, gamma, etc.
  2. Support for utility functions like shuffle. perm, etc.
  3. Bernoulli trial, i.e. obtaining true with a probability of p in [0.0, 1.0), and other functions. (Suggestions welcome in the Discord server in the #v-development channel)

@hungrybluedev hungrybluedev marked this pull request as ready for review June 9, 2020 11:20
vlib/rand/README.md Outdated Show resolved Hide resolved
vlib/rand/README.md Outdated Show resolved Hide resolved
vlib/rand/README.md Outdated Show resolved Hide resolved
vlib/rand/README.md Outdated Show resolved Hide resolved
vlib/rand/README.md Outdated Show resolved Hide resolved
vlib/rand/rand.v Outdated Show resolved Hide resolved
@medvednikov medvednikov merged commit e649cf8 into vlang:master Jun 9, 2020
@@ -18,8 +18,7 @@ fn htonl64(payload_len u64) byteptr {
}

fn create_masking_key() []byte {
mut rnd := pcg32.PCG32RNG{}
mask_bit := byte(rnd.int())
mask_bit := byte(rand.intn(255))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rand.intn(max) is exclusive of max. So it should be rand.intn(256). But honestly it doesn't make a practical difference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants