1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

rand: document all functions, document range notation form, fix copy-pasta (#7619)

This commit is contained in:
Larpon
2020-12-27 19:06:17 +01:00
committed by GitHub
parent b9df7aae4d
commit ed6ba0a2b8
8 changed files with 169 additions and 154 deletions

View File

@@ -5,7 +5,7 @@ module pcg32
import rand.util
// Ported from http://www.pcg-random.org/download.html,
// PCG32RNG ported from http://www.pcg-random.org/download.html,
// https://github.com/imneme/pcg-c-basic/blob/master/pcg_basic.c, and
// https://github.com/imneme/pcg-c-basic/blob/master/pcg_basic.h
pub struct PCG32RNG {
@@ -14,12 +14,12 @@ mut:
inc u64 = u64(0xda3e39cb94b95bdb) ^ util.time_seed_64()
}
// rng.seed(seed_data) - seed the PCG32RNG with 4 u32 values.
// The first 2 represent the 64-bit initial state as [lower 32 bits, higher 32 bits]
// seed seeds the PCG32RNG with 4 `u32` values.
// The first 2 represent the 64-bit initial state as `[lower 32 bits, higher 32 bits]`
// The last 2 represent the 64-bit stream/step of the PRNG.
pub fn (mut rng PCG32RNG) seed(seed_data []u32) {
if seed_data.len != 4 {
eprintln('PCG32RNG needs 4 u32s to be seeded. First two the initial state and the last two the stream/step. Both in little endian format: [lower, higher]')
eprintln('PCG32RNG needs 4 u32s to be seeded. First two the initial state and the last two the stream/step. Both in little endian format: [lower, higher].')
exit(1)
}
init_state := u64(seed_data[0]) | (u64(seed_data[1]) << 32)
@@ -31,7 +31,7 @@ pub fn (mut rng PCG32RNG) seed(seed_data []u32) {
rng.u32()
}
// rng.u32() - return a pseudorandom 32 bit unsigned u32
// u32 returns a pseudorandom unsigned `u32`.
[inline]
pub fn (mut rng PCG32RNG) u32() u32 {
oldstate := rng.state
@@ -41,13 +41,13 @@ pub fn (mut rng PCG32RNG) u32() u32 {
return ((xorshifted >> rot) | (xorshifted << ((-rot) & u32(31))))
}
// rng.u64() - return a pseudorandom 64 bit unsigned u64
// u64 returns a pseudorandom 64-bit unsigned `u64`.
[inline]
pub fn (mut rng PCG32RNG) u64() u64 {
return u64(rng.u32()) | (u64(rng.u32()) << 32)
}
// rn.u32n(max) - return a pseudorandom 32 bit unsigned u32 in [0, max)
// u32n returns a pseudorandom 32-bit unsigned `u32` in range `[0, max)`.
[inline]
pub fn (mut rng PCG32RNG) u32n(max u32) u32 {
if max == 0 {
@@ -71,7 +71,7 @@ pub fn (mut rng PCG32RNG) u32n(max u32) u32 {
return u32(0)
}
// rn.u64n(max) - return a pseudorandom 64 bit unsigned u64 in [0, max)
// u64n returns a pseudorandom 64-bit unsigned `u64` in range `[0, max)`.
[inline]
pub fn (mut rng PCG32RNG) u64n(max u64) u64 {
if max == 0 {
@@ -88,7 +88,7 @@ pub fn (mut rng PCG32RNG) u64n(max u64) u64 {
return u64(0)
}
// rn.u32_in_range(min, max) - return a pseudorandom 32 bit unsigned u32 in [min, max)
// u32_in_range returns a pseudorandom 32-bit unsigned `u32` in range `[min, max)`.
[inline]
pub fn (mut rng PCG32RNG) u32_in_range(min u64, max u64) u64 {
if max <= min {
@@ -98,7 +98,7 @@ pub fn (mut rng PCG32RNG) u32_in_range(min u64, max u64) u64 {
return min + rng.u32n(u32(max - min))
}
// rn.u64_in_range(min, max) - return a pseudorandom 64 bit unsigned u64 in [min, max)
// u64_in_range returns a pseudorandom 64-bit unsigned `u64` in range `[min, max)`.
[inline]
pub fn (mut rng PCG32RNG) u64_in_range(min u64, max u64) u64 {
if max <= min {
@@ -108,31 +108,31 @@ pub fn (mut rng PCG32RNG) u64_in_range(min u64, max u64) u64 {
return min + rng.u64n(max - min)
}
// rng.int() - return a 32-bit signed (possibly negative) int
// int returns a 32-bit signed (possibly negative) `int`.
[inline]
pub fn (mut rng PCG32RNG) int() int {
return int(rng.u32())
}
// rng.i64() - return a 64-bit signed (possibly negative) i64
// i64 returns a 64-bit signed (possibly negative) `i64`.
[inline]
pub fn (mut rng PCG32RNG) i64() i64 {
return i64(rng.u64())
}
// rng.int31() - return a 31bit positive pseudorandom integer
// int31 returns a 31-bit positive pseudorandom `int`.
[inline]
pub fn (mut rng PCG32RNG) int31() int {
return int(rng.u32() >> 1)
}
// rng.int63() - return a 63bit positive pseudorandom integer
// int63 returns a 63-bit positive pseudorandom `i64`.
[inline]
pub fn (mut rng PCG32RNG) int63() i64 {
return i64(rng.u64() >> 1)
}
// rng.intn(max) - return a 32bit positive int in [0, max)
// intn returns a 32-bit positive `int` in range `[0, max)`.
[inline]
pub fn (mut rng PCG32RNG) intn(max int) int {
if max <= 0 {
@@ -142,7 +142,7 @@ pub fn (mut rng PCG32RNG) intn(max int) int {
return int(rng.u32n(u32(max)))
}
// rng.i64n(max) - return a 64bit positive i64 in [0, max)
// i64n returns a 64-bit positive `i64` in range `[0, max)`.
[inline]
pub fn (mut rng PCG32RNG) i64n(max i64) i64 {
if max <= 0 {
@@ -152,7 +152,7 @@ pub fn (mut rng PCG32RNG) i64n(max i64) i64 {
return i64(rng.u64n(u64(max)))
}
// rng.int_in_range(min, max) - return a 32bit positive int in [0, max)
// int_in_range returns a 32-bit positive `int` in range `[0, max)`.
[inline]
pub fn (mut rng PCG32RNG) int_in_range(min int, max int) int {
if max <= min {
@@ -162,7 +162,7 @@ pub fn (mut rng PCG32RNG) int_in_range(min int, max int) int {
return min + rng.intn(max - min)
}
// rng.i64_in_range(min, max) - return a 64bit positive i64 in [0, max)
// i64_in_range returns a 64-bit positive `i64` in range `[0, max)`.
[inline]
pub fn (mut rng PCG32RNG) i64_in_range(min i64, max i64) i64 {
if max <= min {
@@ -172,19 +172,19 @@ pub fn (mut rng PCG32RNG) i64_in_range(min i64, max i64) i64 {
return min + rng.i64n(max - min)
}
// rng.f32() returns a pseudorandom f32 value between 0.0 (inclusive) and 1.0 (exclusive) i.e [0, 1)
// f32 returns a pseudorandom `f32` value in range `[0, 1)`.
[inline]
pub fn (mut rng PCG32RNG) f32() f32 {
return f32(rng.u32()) / util.max_u32_as_f32
}
// rng.f64() returns a pseudorandom f64 value between 0.0 (inclusive) and 1.0 (exclusive) i.e [0, 1)
// f64 returns a pseudorandom `f64` value in range `[0, 1)`.
[inline]
pub fn (mut rng PCG32RNG) f64() f64 {
return f64(rng.u64()) / util.max_u64_as_f64
}
// rng.f32n() returns a pseudorandom f32 value in [0, max)
// f32n returns a pseudorandom `f32` value in range `[0, max)`.
[inline]
pub fn (mut rng PCG32RNG) f32n(max f32) f32 {
if max <= 0 {
@@ -194,7 +194,7 @@ pub fn (mut rng PCG32RNG) f32n(max f32) f32 {
return rng.f32() * max
}
// rng.f64n() returns a pseudorandom f64 value in [0, max)
// f64n returns a pseudorandom `f64` value in range `[0, max)`.
[inline]
pub fn (mut rng PCG32RNG) f64n(max f64) f64 {
if max <= 0 {
@@ -204,7 +204,7 @@ pub fn (mut rng PCG32RNG) f64n(max f64) f64 {
return rng.f64() * max
}
// rng.f32_in_range(min, max) returns a pseudorandom f32 that lies in [min, max)
// f32_in_range returns a pseudorandom `f32` in range `[min, max)`.
[inline]
pub fn (mut rng PCG32RNG) f32_in_range(min f32, max f32) f32 {
if max <= min {
@@ -214,7 +214,7 @@ pub fn (mut rng PCG32RNG) f32_in_range(min f32, max f32) f32 {
return min + rng.f32n(max - min)
}
// rng.i64_in_range(min, max) returns a pseudorandom i64 that lies in [min, max)
// i64_in_range returns a pseudorandom `i64` in range `[min, max)`.
[inline]
pub fn (mut rng PCG32RNG) f64_in_range(min f64, max f64) f64 {
if max <= min {