3647fb4def | ||
---|---|---|
.. | ||
buffer | ||
config | ||
constants | ||
mt19937 | ||
musl | ||
pcg32 | ||
seed | ||
splitmix64 | ||
sys | ||
wyrand | ||
README.md | ||
dist_test.v | ||
mini_math.v | ||
rand.c.v | ||
rand.js.v | ||
rand.v | ||
rand_test.js.v | ||
random_bytes_test.v | ||
random_identifiers_test.v | ||
random_numbers_test.v |
README.md
Description
The V rand
module provides two main ways in which users can generate pseudorandom numbers:
Direct Access Through The rand
Module
// Import the rand module
import rand
...
// Optionally seed the default generator
rand.seed([u32(3110), 50714])
...
// Use the top-level functions
rand.u32n(100)?
rand.int() // among others ...
Through A Generator Of Choice
// Import the rand module
import rand
import rand.seed
// Import the module of the generator you want to use
import rand.pcg32
...
// Initialise the generator struct (note the `mut`)
mut rng := &rand.PRNG(pcg32.PCG32RNG{})
// Optionally seed the generator
rng.seed(seed.time_seed_array(pcg32.seed_len))
...
// Use functions of your choice
rng.u32n(100)?
rng.int() // among others ...
More Information
You can change the default generator to a different one. The only requirement is that
the generator must implement the PRNG
interface. See get_current_rng()
and set_rng()
.
Note: The global PRNG is not thread safe. It is recommended to use separate generators for separate threads in multi-threaded applications.
There are only a few extra functions that are defined only in this top-level rand
module.
Otherwise, there is feature parity between the generator functions and the top-level functions.
General Background
A PRNG is a Pseudo Random Number Generator. Computers cannot generate truly random numbers without an external source of noise or entropy. We can use algorithms to generate sequences of seemingly random numbers, but their outputs will always be deterministic. This is often useful for simulations that need the same starting seed.
If you need truly random numbers that are going to be used for cryptography,
use the crypto.rand
module.
Seeding Functions
All the generators are time-seeded.
The helper functions publicly available in rand.seed
module are:
time_seed_array()
- returns a[]u32
that can be directly plugged into theseed()
functions.time_seed_32()
andtime_seed_64()
- 32-bit and 64-bit values respectively that are generated from the current time.
Caveats
Note that the sys.SysRNG
struct (in the C backend) uses C.srand()
which sets the seed globally.
Consequently, all instances of the RNG will be affected.
This problem does not arise for the other RNGs.
A workaround (if you must use the libc RNG) is to:
- Seed the first instance.
- Generate all values required.
- Seed the second instance.
- Generate all values required.
- And so on...
Notes
Please note that math interval notation is used throughout
the function documentation to denote what numbers ranges include.
An example of [0, max)
thus denotes a range with all posible values
between 0
and max
including 0 but excluding max
.