core/math/rand
rand
Types
3PCG_Random_State
PCG_Random_State :: struct {
state: u64,
inc: u64,
}SourceThe state for a PCG64 RXS-M-XS pseudorandom generator.
Xoshiro256_Random_State
Xoshiro256_Random_State :: struct {
s: [4]u64,
}SourceThe state for a xoshiro256** pseudorandom generator.
Zipf
Zipf :: struct {
gen: runtime.Random_Generator,
imax: f64,
v: f64,
q: f64,
s: f64,
oneminus_Q: f64,
oneminus_Qinv: f64,
hxm: f64,
hx0_minus_hxm: f64,
}SourceA contextual structure for generating Zipf distributed variates.
Constants
4Default_Random_State
Default_Random_State :: runtime.Default_Random_StateSourceGenerator
Generator :: runtime.Random_GeneratorSourceGenerator_Query_Info
Generator_Query_Info :: runtime.Random_Generator_Query_InfoSourcedefault_random_generator
default_random_generator :: runtime.default_random_generatorSourceReturns an instance of the runtime pseudorandom generator. If no initial state is provided, the PRNG will be lazily initialized with entropy from the system entropy source on first-use.
The cryptographic security of the returned random number generator is directly dependent on the quality of the initialization entropy. Calling reset/create SHOULD be done with no seed/state, or 32-bytes of high-quality entropy.
WARNING:
- The lazy initialization will panic if there is no system entropy
source available.
- While the generator is cryptographically secure, developers SHOULD
prefer crypto.random_generator() for cryptographic use cases such as key generation.
Inputs:
- state: Optional initial PRNG state.
Returns:
- A
Generatorinstance.
Procedures
75choice
choice :: proc(array: T, gen = context.random_generator) -> (res: E)SourceReturns a random element from the provided slice. If no generator is provided the global random number generator will be used.
Inputs:
- array: The slice to choose an element from
Returns:
- res: A random element from
array
Example:
import "core:math/rand"
import "core:fmt"
choice_example :: proc() {
data: [4]int = { 1, 2, 3, 4 }
fmt.println(rand.choice(data[:]))
fmt.println(rand.choice(data[:]))
fmt.println(rand.choice(data[:]))
fmt.println(rand.choice(data[:]))
}Possible Output:
3
2
2
4choice_bit_set
choice_bit_set :: proc(set: T, gen = context.random_generator) -> (res: E, ok: bool)SourceReturns a random set bit from the provided bit_set.
Inputs:
- set: The
bit_setto choose a random set bit from
Returns:
- res: The randomly selected bit, or the zero value if
okisfalse - ok: Whether the bit_set was not empty and thus
resis actually a random set bit
Example:
import "core:math/rand"
import "core:fmt"
choice_bit_set_example :: proc() {
Flags :: enum {
A,
B = 10,
C,
}
fmt.println(rand.choice_bit_set(bit_set[Flags]{}))
fmt.println(rand.choice_bit_set(bit_set[Flags]{.B}))
fmt.println(rand.choice_bit_set(bit_set[Flags]{.B, .C}))
fmt.println(rand.choice_bit_set(bit_set[0..<15]{5, 1, 4}))
}Possible Output:
A false
B true
C true
5 truechoice_enum
choice_enum :: proc(T: typeid, gen = context.random_generator) -> (T)Sourcecreate_bytes
create_bytes :: proc(seed: []u8) -> (state: Default_Random_State)Sourcecreate_u64
create_u64 :: proc(seed: u64) -> (state: Default_Random_State)Sourceexp_float64
exp_float64 :: proc(gen = context.random_generator) -> (f64)Sourceexp_float64 returns a exponential distribution in the range (0, max(f64)], with an exponential distribution who rate parameter is 1 (lambda) and whose mean is 1 (1/lambda).
To produce a distribution with a differetn rate parameter, divide the result by the desired rate parameter
"The Ziggurat Method for Generating Random Variables" Authors: George Marsaglia, Wai Wan Tsang Submitted: 2000-04-15. Published: 2000-10-02. https://www.jstatsoft.org/index.php/jss/article/view/v005i08/ziggurat.pdf [pdf] https://www.jstatsoft.org/article/view/v005i08 [web page]
float32
float32 :: proc(gen = context.random_generator) -> (val: f32)SourceGenerates a random single floating point value in the range [0, 1) using the provided random number generator. If no generator is provided the global random number generator will be used.
Returns:
- val: A random single floating point value in the range
[0, 1)
Example:
import "core:math/rand"
import "core:fmt"
float32_example :: proc() {
fmt.println(rand.float32())
}Possible Output:
0.043
0.511float32_beta
float32_beta :: proc(alpha: f32, beta: f32, gen = context.random_generator) -> (f32)SourceBeta Distribution
Required: alpha > 0 and beta > 0
Return values range between 0 and 1
float32_cauchy_lorentz
float32_cauchy_lorentz :: proc(x_0: f32, gamma: f32, gen = context.random_generator) -> (f32)SourceCauchy-Lorentz Distribution x_0 is the location, gamma is the scale where gamma > 0
float32_exponential
float32_exponential :: proc(lambda: f32, gen = context.random_generator) -> (f32)SourceExponential Distribution lambda is 1.0/(desired mean). It should be non-zero. Return values range from 0 to positive infinity if lambda > 0 negative infinity to 0 if lambda <= 0
float32_gamma
float32_gamma :: proc(alpha: f32, beta: f32, gen = context.random_generator) -> (f32)SourceGamma Distribution (NOT THE GAMMA FUNCTION)
Required: alpha > 0 and beta > 0
math.pow(x, alpha-1) math.exp(-x / beta) pdf(x) = -------------------------------------------- math.gamma(alpha) math.pow(beta, alpha)
mean is alphabeta, variance is math.pow(alphabeta, 2)
float32_gompertz
float32_gompertz :: proc(eta: f32, b: f32, gen = context.random_generator) -> (f32)SourceGompertz Distribution eta is the shape, b is the scale Both eta and b must be > 0
float32_laplace
float32_laplace :: proc(mean: f32, b: f32, gen = context.random_generator) -> (f32)SourceLaplace Distribution b is the scale where b > 0
float32_log_cauchy_lorentz
float32_log_cauchy_lorentz :: proc(x_0: f32, gamma: f32, gen = context.random_generator) -> (f32)SourceLog Cauchy-Lorentz Distribution x_0 is the location, gamma is the scale where gamma > 0
float32_log_normal
float32_log_normal :: proc(mean: f32, stddev: f32, gen = context.random_generator) -> (f32)SourceLog Normal Distribution
float32_normal
float32_normal :: proc(mean: f32, stddev: f32, gen = context.random_generator) -> (f32)SourceNormal/Gaussian Distribution
float32_pareto
float32_pareto :: proc(alpha: f32, beta: f32, gen = context.random_generator) -> (f32)SourcePareto distribution, alpha is the shape parameter. https://wikipedia.org/wiki/Pareto_distribution
float32_range
float32_range :: proc(low: f32, high: f32, gen = context.random_generator) -> (val: f32)SourceGenerates a random single floating point value in the range [low, high) using the provided random number generator. If no generator is provided the global random number generator will be used.
Inputs:
- low: The lower bounds of the value, this value is inclusive
- high: The upper bounds of the value, this value is exclusive
Returns:
- val: A random single floating point value in the range [low, high)
WARNING: Panics if high < low
Example:
import "core:math/rand"
import "core:fmt"
float32_range_example :: proc() {
fmt.println(rand.float32_range(-10, 300))
}Possible Output:
15.312
273.15float32_triangular
float32_triangular :: proc(lo: f32, hi: f32, mode: Maybe(f32), gen = context.random_generator) -> (f32)SourceTriangular Distribution See: http://wikipedia.org/wiki/Triangular_distribution
float32_uniform
float32_uniform :: proc(low: f32, high: f32, gen = context.random_generator) -> (val: f32)Sourcefloat32_von_mises
float32_von_mises :: proc(mean_angle: f32, kappa: f32, gen = context.random_generator) -> (f32)SourceCircular Data (von Mises) Distribution mean_angle is the in mean angle between 0 and 2pi radians kappa is the concentration parameter which must be >= 0 When kappa is zero, the Distribution is a uniform Distribution over the range 0 to 2pi
float32_weibull
float32_weibull :: proc(alpha: f32, beta: f32, gen = context.random_generator) -> (f32)SourceWeibull distribution, alpha is the scale parameter, beta is the shape parameter.
float64
float64 :: proc(gen = context.random_generator) -> (val: f64)SourceGenerates a random double floating point value in the range [0, 1) using the provided random number generator. If no generator is provided the global random number generator will be used.
Returns:
- val: A random double floating point value in the range
[0, 1)
Example:
import "core:math/rand"
import "core:fmt"
float64_example :: proc() {
fmt.println(rand.float64())
}Possible Output:
0.043
0.511float64_beta
float64_beta :: proc(alpha: f64, beta: f64, gen = context.random_generator) -> (f64)SourceBeta Distribution
Required: alpha > 0 and beta > 0
Return values range between 0 and 1
float64_cauchy_lorentz
float64_cauchy_lorentz :: proc(x_0: f64, gamma: f64, gen = context.random_generator) -> (f64)SourceCauchy-Lorentz Distribution x_0 is the location, gamma is the scale where gamma > 0
float64_exponential
float64_exponential :: proc(lambda: f64, gen = context.random_generator) -> (f64)SourceExponential Distribution lambda is 1.0/(desired mean). It should be non-zero. Return values range from 0 to positive infinity if lambda > 0 negative infinity to 0 if lambda <= 0
float64_gamma
float64_gamma :: proc(alpha: f64, beta: f64, gen = context.random_generator) -> (f64)SourceGamma Distribution (NOT THE GAMMA FUNCTION)
Required: alpha > 0 and beta > 0
math.pow(x, alpha-1) math.exp(-x / beta) pdf(x) = -------------------------------------------- math.gamma(alpha) math.pow(beta, alpha)
mean is alphabeta, variance is math.pow(alphabeta, 2)
float64_gompertz
float64_gompertz :: proc(eta: f64, b: f64, gen = context.random_generator) -> (f64)SourceGompertz Distribution eta is the shape, b is the scale Both eta and b must be > 0
float64_laplace
float64_laplace :: proc(mean: f64, b: f64, gen = context.random_generator) -> (f64)SourceLaplace Distribution b is the scale where b > 0
float64_log_cauchy_lorentz
float64_log_cauchy_lorentz :: proc(x_0: f64, gamma: f64, gen = context.random_generator) -> (f64)SourceLog Cauchy-Lorentz Distribution x_0 is the location, gamma is the scale where gamma > 0
float64_log_normal
float64_log_normal :: proc(mean: f64, stddev: f64, gen = context.random_generator) -> (f64)SourceLog Normal Distribution
float64_normal
float64_normal :: proc(mean: f64, stddev: f64, gen = context.random_generator) -> (f64)SourceNormal/Gaussian Distribution
float64_pareto
float64_pareto :: proc(alpha: f64, gen = context.random_generator) -> (f64)SourcePareto distribution, alpha is the shape parameter. https://wikipedia.org/wiki/Pareto_distribution
float64_range
float64_range :: proc(low: f64, high: f64, gen = context.random_generator) -> (val: f64)SourceGenerates a random double floating point value in the range [low, high) using the provided random number generator. If no generator is provided the global random number generator will be used.
WARNING: Panics if high < low
Inputs:
- low: The lower bounds of the value, this value is inclusive
- high: The upper bounds of the value, this value is exclusive
Returns:
- val: A random double floating point value in the range [low, high)
Example:
import "core:math/rand"
import "core:fmt"
float64_range_example :: proc() {
fmt.println(rand.float64_range(-10, 300))
}Possible Output:
15.312
273.15float64_triangular
float64_triangular :: proc(lo: f64, hi: f64, mode: Maybe(f64), gen = context.random_generator) -> (f64)SourceTriangular Distribution See: http://wikipedia.org/wiki/Triangular_distribution
float64_uniform
float64_uniform :: proc(low: f64, high: f64, gen = context.random_generator) -> (val: f64)Sourcefloat64_von_mises
float64_von_mises :: proc(mean_angle: f64, kappa: f64, gen = context.random_generator) -> (f64)SourceCircular Data (von Mises) Distribution mean_angle is the in mean angle between 0 and 2pi radians kappa is the concentration parameter which must be >= 0 When kappa is zero, the Distribution is a uniform Distribution over the range 0 to 2pi
float64_weibull
float64_weibull :: proc(alpha: f64, beta: f64, gen = context.random_generator) -> (f64)SourceWeibull distribution, alpha is the scale parameter, beta is the shape parameter.
int127
int127 :: proc(gen = context.random_generator) -> (val: i128)SourceGenerates a random 127 bit value using the provided random number generator. If no generator is provided the global random number generator will be used. The sign bit will always be set to 0, thus all generated numbers will be positive.
Returns:
- val: A random 127 bit value
Example:
import "core:math/rand"
import "core:fmt"
int127_example :: proc() {
fmt.println(rand.int127())
}Possible Output:
10
389int127_max
int127_max :: proc(n: i128, gen = context.random_generator) -> (val: i128)SourceGenerates a random 127 bit value in the range [0, n) using the provided random number generator. If no generator is provided the global random number generator will be used.
Inputs:
- n: The upper bound of the generated number, this value is exclusive
Returns:
- val: A random 127 bit value in the range
[0, n)
WARNING: Panics if n is less than or equal to 0
Example:
import "core:math/rand"
import "core:fmt"
int127_max_example :: proc() {
fmt.println(rand.int127_max(16))
}Possible Output:
6
13int128_range
int128_range :: proc(lo: i128, hi: i128, gen = context.random_generator) -> (val: i128)SourceGenerates a random signed 128 bit value in the range [lo, hi) using the provided random number generator. If no generator is provided the global random number generator will be used.
Inputs:
- lo: The lower bound of the generated number, this value is inclusice
- hi: The upper bound of the generated number, this value is exclusive
Returns:
- val: A random 128 bit value in the range
[lo, hi)
WARNING: Panics if lo is greater or equal to hi
Example:
import "core:math/rand"
import "core:fmt"
int128_range_example :: proc() {
fmt.println(rand.int128_range(-10,10))
}Possible Output:
6
-9int31
int31 :: proc(gen = context.random_generator) -> (val: i32)SourceGenerates a random 31 bit value using the provided random number generator. If no generator is provided the global random number generator will be used. The sign bit will always be set to 0, thus all generated numbers will be positive.
Returns:
- val: A random 31 bit value
Example:
import "core:math/rand"
import "core:fmt"
int31_example :: proc() {
fmt.println(rand.int31())
}Possible Output:
10
389int31_max
int31_max :: proc(n: i32, gen = context.random_generator) -> (val: i32)SourceGenerates a random 31 bit value in the range [0, n) using the provided random number generator. If no generator is provided the global random number generator will be used.
Inputs:
- n: The upper bound of the generated number, this value is exclusive
Returns:
- val: A random 31 bit value in the range
[0, n)
WARNING: Panics if n is less than or equal to 0
Example:
import "core:math/rand"
import "core:fmt"
int31_max_example :: proc() {
fmt.println(rand.int31_max(16))
}Possible Output:
6
13int32_range
int32_range :: proc(lo: i32, hi: i32, gen = context.random_generator) -> (val: i32)SourceGenerates a random signed 32 bit value in the range [lo, hi) using the provided random number generator. If no generator is provided the global random number generator will be used.
Inputs:
- lo: The lower bound of the generated number, this value is inclusice
- hi: The upper bound of the generated number, this value is exclusive
Returns:
- val: A random 32 bit value in the range
[lo, hi)
WARNING: Panics if lo is greater or equal to hi
Example:
import "core:math/rand"
import "core:fmt"
int32_range_example :: proc() {
fmt.println(rand.int32_range(-10,10))
}Possible Output:
6
-9int63
int63 :: proc(gen = context.random_generator) -> (val: i64)SourceGenerates a random 63 bit value using the provided random number generator. If no generator is provided the global random number generator will be used. The sign bit will always be set to 0, thus all generated numbers will be positive.
Returns:
- val: A random 63 bit value
Example:
import "core:math/rand"
import "core:fmt"
int63_example :: proc() {
fmt.println(rand.int63())
}Possible Output:
10
389int63_max
int63_max :: proc(n: i64, gen = context.random_generator) -> (val: i64)SourceGenerates a random 63 bit value in the range [0, n) using the provided random number generator. If no generator is provided the global random number generator will be used.
Inputs:
- n: The upper bound of the generated number, this value is exclusive
Returns:
- val: A random 63 bit value in the range
[0, n)
WARNING: Panics if n is less than or equal to 0
Example:
import "core:math/rand"
import "core:fmt"
int63_max_example :: proc() {
fmt.println(rand.int63_max(16))
}Possible Output:
6
13int64_range
int64_range :: proc(lo: i64, hi: i64, gen = context.random_generator) -> (val: i64)SourceGenerates a random signed 64 bit value in the range [lo, hi) using the provided random number generator. If no generator is provided the global random number generator will be used.
Inputs:
- lo: The lower bound of the generated number, this value is inclusice
- hi: The upper bound of the generated number, this value is exclusive
Returns:
- val: A random 64 bit value in the range
[lo, hi)
WARNING: Panics if lo is greater or equal to hi
Example:
import "core:math/rand"
import "core:fmt"
int64_range_example :: proc() {
fmt.println(rand.int64_range(-10,10))
}Possible Output:
6
-9int_max
int_max :: proc(n: int, gen = context.random_generator) -> (val: int)SourceGenerates a random integer value in the range [0, n) using the provided random number generator. If no generator is provided the global random number generator will be used.
Inputs:
- n: The upper bound of the generated number, this value is exclusive
Returns:
- val: A random integer value in the range
[0, n)
WARNING: Panics if n is less than or equal to 0
Example:
import "core:math/rand"
import "core:fmt"
int_max_example :: proc() {
fmt.println(rand.int_max(16))
}Possible Output:
6
13int_range
int_range :: proc(lo: int, hi: int, gen = context.random_generator) -> (val: int)SourceGenerates a random signed integer value in the range [lo, hi) using the provided random number generator. If no generator is provided the global random number generator will be used.
Inputs:
- lo: The lower bound of the generated number, this value is inclusice
- hi: The upper bound of the generated number, this value is exclusive
Returns:
- val: A random integer value in the range
[lo, hi)
WARNING: Panics if lo is greater or equal to hi
Example:
import "core:math/rand"
import "core:fmt"
int_range_example :: proc() {
fmt.println(rand.int_range(-10,10))
}Possible Output:
6
-9norm_float64
norm_float64 :: proc(gen = context.random_generator) -> (f64)Sourcenorm_float64 returns a normally distributed f64 in the range -max(f64) through +max(f64) inclusive, with a standard normal distribution with a mean of 0 and standard deviation of 1.
sample = norm_float64() * std_dev + mean
Normal distribution
"The Ziggurat Method for Generating Random Variables" Authors: George Marsaglia, Wai Wan Tsang Submitted: 2000-04-15. Published: 2000-10-02. https://www.jstatsoft.org/index.php/jss/article/view/v005i08/ziggurat.pdf [pdf] https://www.jstatsoft.org/article/view/v005i08 [web page]
pcg_random_generator
pcg_random_generator :: proc(state: ^PCG_Random_State) -> (Generator)SourceReturns an instance of the PGC64 RXS-M-XS pseudorandom generator. If no initial state is provided, the PRNG will be lazily initialized with the system timestamp counter on first-use.
WARNING: This random number generator is NOT cryptographically secure, and is additionally known to be flawed. It is only included for backward compatibility with historical releases of Odin. See: https://github.com/odin-lang/Odin/issues/5881
Inputs:
- state: Optional initial PRNG state.
Returns:
- A
Generatorinstance.
pcg_random_generator_proc
pcg_random_generator_proc :: proc(data: rawptr, mode: runtime.Random_Generator_Mode, p: []u8)Sourceperm
perm :: proc(n: int, allocator: mem.Allocator = context.allocator, gen = context.random_generator) -> (res: []int, err: runtime.Allocator_Error)SourceCreates a slice of int filled with random values using the provided random number generator. If no generator is provided the global random number generator will be used.
Allocates Using Provided Allocator
Inputs:
- n: The size of the created slice
- allocator: (default: context.allocator)
Returns:
- res: A slice filled with random values
- err: An allocator error if one occured,
nilotherwise
Example:
import "base:runtime"
import "core:math/rand"
import "core:fmt"
perm_example :: proc() -> (err: runtime.Allocator_Error) {
data := rand.perm(4) or_return
fmt.println(data)
defer delete(data, context.allocator)
return
}Possible Output:
[3, 2, 0, 1]
[1, 0, 2, 3]query_info
query_info :: proc(gen = context.random_generator) -> (Generator_Query_Info)Sourceread
read :: proc(p: []u8, gen = context.random_generator) -> (n: int)SourceFills a byte slice with random values using the provided random number generator. If no generator is provided the global random number generator will be used. Due to floating point precision there is no guarantee if the upper and lower bounds are inclusive/exclusive with the exact floating point value.
Inputs:
- p: The byte slice to fill
Returns:
- n: The number of bytes generated
Example:
import "core:math/rand"
import "core:fmt"
read_example :: proc() {
data: [8]byte
n := rand.read(data[:])
fmt.println(n)
fmt.println(data)
}Possible Output:
8
[32, 4, 59, 7, 1, 2, 2, 119]reset_bytes
reset_bytes :: proc(bytes: []u8, gen = context.random_generator)Sourcereset_u64
reset_u64 :: proc(seed: u64, gen = context.random_generator)Sourceshuffle
shuffle :: proc(array: T, gen = context.random_generator)SourceRandomizes the ordering of elements for the provided slice. If no generator is provided the global random number generator will be used.
Inputs:
- array: The slice to randomize
Example:
import "core:math/rand"
import "core:fmt"
shuffle_example :: proc() {
data: [4]int = { 1, 2, 3, 4 }
fmt.println(data) // the contents are in order
rand.shuffle(data[:])
fmt.println(data) // the contents have been shuffled
}Possible Output:
[1, 2, 3, 4]
[2, 4, 3, 1]uint128
uint128 :: proc(gen = context.random_generator) -> (val: u128)SourceGenerates a random 128 bit value using the provided random number generator. If no generator is provided the global random number generator will be used.
Returns:
- val: A random unsigned 128 bit value
Example:
import "core:math/rand"
import "core:fmt"
uint128_example :: proc() {
fmt.println(rand.uint128())
}Possible Output:
10
389uint128_max
uint128_max :: proc(n: u128, gen = context.random_generator) -> (val: u128)SourceGenerates a random 128 bit value in the range [0, n) using the provided random number generator. If no generator is provided the global random number generator will be used.
Inputs:
- n: The upper bound of the generated number, this value is exclusive
Returns:
- val: A random 128 bit value in the range
[0, n)
WARNING: Panics if n is equal to 0
Example:
import "core:math/rand"
import "core:fmt"
uint128_max_example :: proc() {
fmt.println(rand.uint128_max(16))
}Possible Output:
6
13uint128_range
uint128_range :: proc(lo: u128, hi: u128, gen = context.random_generator) -> (val: u128)SourceGenerates a random unsigned 128 bit value in the range [lo, hi) using the provided random number generator. If no generator is provided the global random number generator will be used.
Inputs:
- lo: The lower bound of the generated number, this value is inclusice
- hi: The upper bound of the generated number, this value is exclusive
Returns:
- val: A random 128 bit value in the range
[lo, hi)
WARNING: Panics if lo is greater or equal to hi
Example:
import "core:math/rand"
import "core:fmt"
uint128_range_example :: proc() {
fmt.println(rand.uint128_range(5,15))
}Possible Output:
6
13uint32
uint32 :: proc(gen = context.random_generator) -> (val: u32)SourceGenerates a random 32 bit value using the provided random number generator. If no generator is provided the global random number generator will be used.
Returns:
- val: A random unsigned 32 bit value
Example:
import "core:math/rand"
import "core:fmt"
uint32_example :: proc() {
fmt.println(rand.uint32())
}Possible Output:
10
389uint32_max
uint32_max :: proc(n: u32, gen = context.random_generator) -> (val: u32)SourceGenerates a random 32 bit value in the range [0, n) using the provided random number generator. If no generator is provided the global random number generator will be used.
Inputs:
- n: The upper bound of the generated number, this value is exclusive
Returns:
- val: A random 32 bit value in the range
[0, n)
WARNING: Panics if n is equal to 0
Example:
import "core:math/rand"
import "core:fmt"
uint32_max_example :: proc() {
fmt.println(rand.uint32_max(16))
}Possible Output:
6
13uint32_range
uint32_range :: proc(lo: u32, hi: u32, gen = context.random_generator) -> (val: u32)SourceGenerates a random unsigned 32 bit value in the range [lo, hi) using the provided random number generator. If no generator is provided the global random number generator will be used.
Inputs:
- lo: The lower bound of the generated number, this value is inclusice
- hi: The upper bound of the generated number, this value is exclusive
Returns:
- val: A random 32 bit value in the range
[lo, hi)
WARNING: Panics if lo is greater or equal to hi
Example:
import "core:math/rand"
import "core:fmt"
uint32_range_example :: proc() {
fmt.println(rand.uint32_range(5,15))
}Possible Output:
6
13uint64
uint64 :: proc(gen = context.random_generator) -> (val: u64)SourceGenerates a random 64 bit value using the provided random number generator. If no generator is provided the global random number generator will be used.
Returns:
- val: A random unsigned 64 bit value
Example:
import "core:math/rand"
import "core:fmt"
uint64_example :: proc() {
fmt.println(rand.uint64())
}Possible Output:
10
389uint64_max
uint64_max :: proc(n: u64, gen = context.random_generator) -> (val: u64)SourceGenerates a random 64 bit value in the range [0, n) using the provided random number generator. If no generator is provided the global random number generator will be used.
Inputs:
- n: The upper bound of the generated number, this value is exclusive
Returns:
- val: A random 64 bit value in the range
[0, n)
WARNING: Panics if n is equal to 0
Example:
import "core:math/rand"
import "core:fmt"
uint64_max_example :: proc() {
fmt.println(rand.uint64_max(16))
}Possible Output:
6
13uint64_range
uint64_range :: proc(lo: u64, hi: u64, gen = context.random_generator) -> (val: u64)SourceGenerates a random unsigned 64 bit value in the range [lo, hi) using the provided random number generator. If no generator is provided the global random number generator will be used.
Inputs:
- lo: The lower bound of the generated number, this value is inclusice
- hi: The upper bound of the generated number, this value is exclusive
Returns:
- val: A random 64 bit value in the range
[lo, hi)
WARNING: Panics if lo is greater or equal to hi
Example:
import "core:math/rand"
import "core:fmt"
uint64_range_example :: proc() {
fmt.println(rand.uint64_range(5,15))
}Possible Output:
6
13uint_max
uint_max :: proc(n: uint, gen = context.random_generator) -> (val: uint)SourceGenerates a random integer value in the range [0, n) using the provided random number generator. If no generator is provided the global random number generator will be used.
Inputs:
- n: The upper bound of the generated number, this value is exclusive
Returns:
- val: A random integer value in the range
[0, n)
WARNING: Panics if n is equal to 0
Example:
import "core:math/rand"
import "core:fmt"
uint_max_example :: proc() {
fmt.println(rand.uint_max(16))
}Possible Output:
6
13uint_range
uint_range :: proc(lo: uint, hi: uint, gen = context.random_generator) -> (val: uint)SourceGenerates a random unsigned integer value in the range [lo, hi) using the provided random number generator. If no generator is provided the global random number generator will be used.
Inputs:
- lo: The lower bound of the generated number, this value is inclusice
- hi: The upper bound of the generated number, this value is exclusive
Returns:
- val: A random integer value in the range
[lo, hi)
WARNING: Panics if lo is greater or equal to hi
Example:
import "core:math/rand"
import "core:fmt"
uint_range_example :: proc() {
fmt.println(rand.uint_range(5,15))
}Possible Output:
6
13xoshiro256_random_generator
xoshiro256_random_generator :: proc(state: ^Xoshiro256_Random_State) -> (Generator)SourceReturns an instance of the xoshiro256** pseudorandom generator. If no initial state is provided, the PRNG will be lazily initialized with the system timestamp counter on first-use.
WARNING: This random number generator is NOT cryptographically secure.
Inputs:
- state: Optional initial PRNG state.
Returns:
- A
Generatorinstance.
xoshiro256_random_generator_proc
xoshiro256_random_generator_proc :: proc(data: rawptr, mode: runtime.Random_Generator_Mode, p: []u8)Sourcezipf_create
zipf_create :: proc(s: f64, v: f64, imax: u64, gen = context.random_generator) -> (z: Zipf, ok: bool)SourceCreates a Zipf variate generator. The generator produces values k ∈ [0, imax] such that P(k) is proportional to (v + k) ** (-s). The parameters must be: s > 1 and v >= 1
W.Hormann, G.Derflinger: "Rejection-Inversion to Generate Variates from Monotone Discrete Distributions" http://eeyore.wu-wien.ac.at/papers/96-04-04.wh-der.ps.gz
zipf_h
zipf_h :: proc(z: Zipf, x: f64) -> (f64)Sourcezipf_hinv
zipf_hinv :: proc(z: Zipf, x: f64) -> (f64)Sourcezipf_uint64
zipf_uint64 :: proc(z: Zipf) -> (u64)SourceReturns a value drawn from the zipf distribution described by the Zipf contextual structure.
Procedure Groups
2create
create :: proc{create_u64, create_bytes}Sourcereset
reset :: proc{reset_u64, reset_bytes}SourceReset the seed used by the context.random_generator.
Inputs:
- seed: The seed value
Example:
import "core:math/rand"
import "core:fmt"
reset_example :: proc() {
rand.reset(1)
fmt.println(rand.uint64())
}Possible Output:
10