Skip to content

Commit 0e54345

Browse files
committed
rand: update rand.seed() documentation, mention that the default rng is already time seeded
1 parent 90278d0 commit 0e54345

1 file changed

Lines changed: 36 additions & 32 deletions

File tree

‎vlib/rand/rand.v‎

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,42 @@ mut:
2323
free()
2424
}
2525

26+
// default_rng is used by the module level public APIs like rand.u8() etc.
27+
__global default_rng &PRNG
28+
29+
// new_default returns a new instance of the default RNG. If the seed is not provided, the current time will be used to seed the instance.
30+
@[manualfree]
31+
pub fn new_default(config_ config.PRNGConfigStruct) &PRNG {
32+
mut rng := &wyrand.WyRandRNG{}
33+
rng.seed(config_.seed_)
34+
unsafe { config_.seed_.free() }
35+
return &PRNG(rng)
36+
}
37+
38+
// get_current_rng returns the PRNG instance currently in use. If it is not changed, it will be an instance of wyrand.WyRandRNG.
39+
pub fn get_current_rng() &PRNG {
40+
return default_rng
41+
}
42+
43+
// set_rng changes the default RNG from wyrand.WyRandRNG (or whatever the last RNG was).
44+
// Note that this new RNG must be seeded manually with a constant seed or the
45+
// `seed.time_seed_array()` method. Also, it is recommended to store the old RNG in a variable and
46+
// should be restored if work with the custom RNG is complete. It is not necessary to restore if the
47+
// program terminates soon afterwards.
48+
pub fn set_rng(rng &PRNG) {
49+
default_rng = unsafe { rng }
50+
}
51+
52+
// seed sets the given array of `u32` values as the seed for the `default_rng`.
53+
// Note: the default_rng is already seeded with a *time dependent value*,
54+
// so if you just need some randomness, for a game/simulation, and you do not need
55+
// reproducibility, just use it, without calling rand.seed() to do explicit seeding.
56+
// The default_rng is an instance of WyRandRNG which takes 2 u32 values.
57+
// When using a custom RNG, make sure to use the correct number of u32s.
58+
pub fn seed(seed []u32) {
59+
default_rng.seed(seed)
60+
}
61+
2662
// bytes returns a buffer of `bytes_needed` random bytes
2763
@[inline]
2864
pub fn (mut rng PRNG) bytes(bytes_needed int) ![]u8 {
@@ -513,38 +549,6 @@ pub fn (mut rng PRNG) sample[T](array []T, k int) []T {
513549
return results
514550
}
515551

516-
__global default_rng &PRNG
517-
518-
// new_default returns a new instance of the default RNG. If the seed is not provided, the current time will be used to seed the instance.
519-
@[manualfree]
520-
pub fn new_default(config_ config.PRNGConfigStruct) &PRNG {
521-
mut rng := &wyrand.WyRandRNG{}
522-
rng.seed(config_.seed_)
523-
unsafe { config_.seed_.free() }
524-
return &PRNG(rng)
525-
}
526-
527-
// get_current_rng returns the PRNG instance currently in use. If it is not changed, it will be an instance of wyrand.WyRandRNG.
528-
pub fn get_current_rng() &PRNG {
529-
return default_rng
530-
}
531-
532-
// set_rng changes the default RNG from wyrand.WyRandRNG (or whatever the last RNG was).
533-
// Note that this new RNG must be seeded manually with a constant seed or the
534-
// `seed.time_seed_array()` method. Also, it is recommended to store the old RNG in a variable and
535-
// should be restored if work with the custom RNG is complete. It is not necessary to restore if the
536-
// program terminates soon afterwards.
537-
pub fn set_rng(rng &PRNG) {
538-
default_rng = unsafe { rng }
539-
}
540-
541-
// seed sets the given array of `u32` values as the seed for the `default_rng`.
542-
// The default_rng is an instance of WyRandRNG which takes 2 u32 values. When using a custom RNG, make sure to use
543-
// the correct number of u32s.
544-
pub fn seed(seed []u32) {
545-
default_rng.seed(seed)
546-
}
547-
548552
// u8 returns a uniformly distributed pseudorandom 8-bit unsigned positive `u8`.
549553
pub fn u8() u8 {
550554
return default_rng.u8()

0 commit comments

Comments
 (0)