diff --git a/src/std/math-rand.md b/src/std/math-rand.md index 2dc6868..d2b8fd6 100644 --- a/src/std/math-rand.md +++ b/src/std/math-rand.md @@ -1,75 +1,72 @@ # std/math/rand -## Type Aliases -```jule -type Seed: u64 -``` -Integer type of random seeds. -## Structures +## Functions ```jule -struct Rand +fn NewSource(seed: u64): Source ``` -This structure implements a type of pseudo random number generator (PRNG). The seed must be given manually for each instance. +Returns new default source by seed. -Seeds: - +If you want to achieve randomness somehow, use a variable seed. A simple solution for seeds that will create the illusion of randomness is to use time. Unix-time seconds would be a simple seed solution. -Ranges: - +```jule +struct Rand +``` +Implements a type of pseudo random number generator (PRNG). Outputs might be easily predictable regardless of how it's seeded. For random numbers suitable for security-sensitive work, it is not recommended. **Methods:** -`static fn New(seed: Seed): &Rand`\ -Returns new PRNG for seed. - ---- +`static fn New(src: Source): &Rand`\ +Returns new `Rand` that uses random values from `src` to generate other random values. -`fn Next63(self): i64`\ -Returns a non-genative pseudo-random 63-bit signed integer as an 64-bit signed integer. +`fn U64(self): u64`\ +Returns a pseudo-random 64-bit value as `u64`. ---- +`fn I64(self): u32`\ +Returns a pseudo-random 32-bit value as `u32`. -`fn Next31(self): i32`\ -Returns a non-genative pseudo-random 31-bit signed integer as an 31-bit signed integer. +`fn I64(self): i64`\ +Returns a non-negative pseudo-random 64-bit value as `i64`. ---- +`fn I32(self): i32`\ +Returns a non-negative pseudo-random 32-bit value as `i32`. -`fn Next(self): int`\ -Returns a non-genative pseudo-random int. +`fn Int(self): int`\ +Returns a non-negative pseudo-random int. ---- +`fn U64n(self, n: u64): u64`\ +Returns, as a `u64`, a non-negative pseudo-random number in the half-open interval `[0,n)`. It panics if `n == 0`. -`fn Nextn63(self, n: i64): i64`\ -Returns a non-genative pseudo-random in `[0, n)` range 63-bit signed integer as an 64-bit signed integer. If `n <= 0`, it panics. +`fn I64n(self, n: i64): i64`\ +Returns, as an `i64`, a non-negative pseudo-random number in the half-open interval `[0,n)`. It panics if `n == 0`. ---- +`fn I32n(self, n: i32): i32`\ +Returns, as an `i32`, a non-negative pseudo-random number in the half-open interval `[0,n)`. It panics if `n <= 0`. -`fn Nextn31(self, n: i32): i32`\ -Returns a non-genative pseudo-random in `[0, n)` range 31-bit signed integer as an 31-bit signed integer. If `n <= 0`, it panics. +`fn U32n(self, n: u32): u32`\ +Returns, as a `u32`, a non-negative pseudo-random number in the half-open interval `[0,n)`. It panics if `n == 0`. ---- +`fn Intn(self, n: int): int`\ +Returns, as an `int`, a non-negative pseudo-random number in the half-open interval `[0,n)`. It panics if `n <= 0`. -`fn Nextn(self, n: int): int`\ -Returns a non-genative pseudo-random in [0, n) range as int. If `n <= 0`, it panics. +`fn Uintn(self, n: uint): uint`\ +Returns, as a `uint`, a non-negative pseudo-random number in the half-open interval `[0,n)`. It panics if `n == 0`. ---- +`fn F64(self): f64`\ +Returns, as a `f64`, a pseudo-random number in the half-open interval `[0.0,1.0)`. -`fn Fnext64(self): f64`\ -Returns a non-genative pseudo-random in `[0.0, 1.0)` range as f64 floating-point. +`fn F32(self): f32`\ +Returns, as a `f32`, a pseudo-random number in the half-open interval `[0.0,1.0)`. ---- +## Traits -`fn Fnext32(self): f32`\ -Returns a non-genative pseudo-random in `[0.0, 1.0)` range as f32 floating-point. \ No newline at end of file +```jule +trait Source { + fn U64(self) +} +``` +Source of uniformly-distributed pseudo-random u64 values in the range `[0, 1<<64)`. It is not safe for concurrent use by multiple threads. \ No newline at end of file