package core:math/rand
Overview
Package core:math/rand implements various random number generators
Index
Types (1)
Constants (0)
This section is empty.
Variables (0)
This section is empty.
Procedures (53)
- choice
- choice_enum
- create
- exp_float64
- float32
- float32_beta
- float32_cauchy_lorentz
- float32_exponential
- float32_gamma
- float32_gompertz
- float32_laplace
- float32_log_cauchy_lorentz
- float32_log_normal
- float32_normal
- float32_pareto
- float32_range
- float32_triangular
- float32_uniform
- float32_von_mises
- float32_weibull
- float64
- float64_beta
- float64_cauchy_lorentz
- float64_exponential
- float64_gamma
- float64_gompertz
- float64_laplace
- float64_log_cauchy_lorentz
- float64_log_normal
- float64_normal
- float64_pareto
- float64_range
- float64_triangular
- float64_uniform
- float64_von_mises
- float64_weibull
- init
- init_as_system
- int127
- int127_max
- int31
- int31_max
- int63
- int63_max
- int_max
- norm_float64
- perm
- read
- set_global_seed
- shuffle
- uint128
- uint32
- uint64
Procedure Groups (0)
This section is empty.
Types
Rand ¶
Related Procedures With Parameters
- choice
- choice_enum
- exp_float64
- float32
- float32_beta
- float32_cauchy_lorentz
- float32_exponential
- float32_gamma
- float32_gompertz
- float32_laplace
- float32_log_cauchy_lorentz
- float32_log_normal
- float32_normal
- float32_pareto
- float32_range
- float32_triangular
- float32_von_mises
- float32_weibull
- float64
- float64_beta
- float64_cauchy_lorentz
- float64_exponential
- float64_gamma
- float64_gompertz
- float64_laplace
- float64_log_cauchy_lorentz
- float64_log_normal
- float64_normal
- float64_pareto
- float64_range
- float64_triangular
- float64_von_mises
- float64_weibull
- init
- init_as_system
- int127
- int127_max
- int31
- int31_max
- int63
- int63_max
- int_max
- norm_float64
- perm
- read
- shuffle
- uint128
- uint32
- uint64
Related Procedures With Returns
Constants
This section is empty.
Variables
This section is empty.
Procedures
choice ¶
choice :: proc(array: $T/[]$T, r: ^Rand = nil) -> (res: $T) {…}
Returns 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
r: The random number generator to use, or nil for the global generator
Returns:
res: A random element from array
Example:
import "core:math/rand"
import "core:fmt"
choice_example :: proc() {
// Using the global random number generator
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 4
create ¶
Creates a new random number generator.
Inputs:
seed: The seed value to create the random number generator with
Returns:
res: The created random number generator
Example:
import "core:math/rand"
import "core:fmt"
create_example :: proc() {
my_rand := rand.create(1)
fmt.println(rand.uint64(&my_rand))
}
Possible Output:10
exp_float64 ¶
exp_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 ¶
Generates 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.
Inputs:
r: The random number generator to use, or nil for the global generator
Returns:
val: A random single floating point value in the range [0, 1)
Example:
import "core:math/rand"
import "core:fmt"
float32_example :: proc() {
// Using the global random number generator
fmt.println(rand.float32())
// Using local random number generator
my_rand := rand.create(1)
fmt.println(rand.float32(&my_rand))
}
Possible Output:0.043 0.511
float32_beta ¶
Beta Distribution
Required: alpha > 0 and beta > 0
Return values range between 0 and 1
float32_cauchy_lorentz ¶
Cauchy-Lorentz Distribution
x_0
is the location, gamma
is the scale where gamma
> 0
float32_exponential ¶
Exponential 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 ¶
Gamma 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 ¶
Gompertz Distribution
eta
is the shape, b
is the scale
Both eta
and b
must be > 0
float32_laplace ¶
Laplace Distribution
b
is the scale where b
> 0
float32_log_cauchy_lorentz ¶
Log Cauchy-Lorentz Distribution
x_0
is the location, gamma
is the scale where gamma
> 0
float32_log_normal ¶
Log Normal Distribution
float32_normal ¶
Normal/Gaussian Distribution
float32_pareto ¶
Pareto distribution, alpha
is the shape parameter.
https://wikipedia.org/wiki/Pareto_distribution
float32_range ¶
Generates 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
r: The random number generator to use, or nil for the global generator
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() {
// Using the global random number generator
fmt.println(rand.float32_range(-10, 300))
// Using local random number generator
my_rand := rand.create(1)
fmt.println(rand.float32_range(600, 900, &my_rand))
}
Possible Output:15.312 673.130
float32_triangular ¶
Triangular Distribution See: http://wikipedia.org/wiki/Triangular_distribution
float32_uniform ¶
float32_uniform :: float32_range
Generates 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
r: The random number generator to use, or nil for the global generator
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() {
// Using the global random number generator
fmt.println(rand.float32_range(-10, 300))
// Using local random number generator
my_rand := rand.create(1)
fmt.println(rand.float32_range(600, 900, &my_rand))
}
Possible Output:15.312 673.130
float32_von_mises ¶
Circular 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 ¶
Weibull distribution, alpha
is the scale parameter, beta
is the shape parameter.
float64 ¶
Generates 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.
Inputs:
r: The random number generator to use, or nil for the global generator
Returns:
val: A random double floating point value in the range [0, 1)
Example:
import "core:math/rand"
import "core:fmt"
float64_example :: proc() {
// Using the global random number generator
fmt.println(rand.float64())
// Using local random number generator
my_rand := rand.create(1)
fmt.println(rand.float64(&my_rand))
}
Possible Output:0.043 0.511
float64_beta ¶
Beta Distribution
Required: alpha > 0 and beta > 0
Return values range between 0 and 1
float64_cauchy_lorentz ¶
Cauchy-Lorentz Distribution
x_0
is the location, gamma
is the scale where gamma
> 0
float64_exponential ¶
Exponential 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 ¶
Gamma 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 ¶
Gompertz Distribution
eta
is the shape, b
is the scale
Both eta
and b
must be > 0
float64_laplace ¶
Laplace Distribution
b
is the scale where b
> 0
float64_log_cauchy_lorentz ¶
Log Cauchy-Lorentz Distribution
x_0
is the location, gamma
is the scale where gamma
> 0
float64_log_normal ¶
Log Normal Distribution
float64_normal ¶
Normal/Gaussian Distribution
float64_pareto ¶
Pareto distribution, alpha
is the shape parameter.
https://wikipedia.org/wiki/Pareto_distribution
float64_range ¶
Generates 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
r: The random number generator to use, or nil for the global generator
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() {
// Using the global random number generator
fmt.println(rand.float64_range(-10, 300))
// Using local random number generator
my_rand := rand.create(1)
fmt.println(rand.float64_range(600, 900, &my_rand))
}
Possible Output:15.312 673.130
float64_triangular ¶
Triangular Distribution See: http://wikipedia.org/wiki/Triangular_distribution
float64_uniform ¶
float64_uniform :: float64_range
Generates 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
r: The random number generator to use, or nil for the global generator
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() {
// Using the global random number generator
fmt.println(rand.float64_range(-10, 300))
// Using local random number generator
my_rand := rand.create(1)
fmt.println(rand.float64_range(600, 900, &my_rand))
}
Possible Output:15.312 673.130
float64_von_mises ¶
Circular 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 ¶
Weibull distribution, alpha
is the scale parameter, beta
is the shape parameter.
init ¶
Initialises a random number generator.
Inputs:
r: The random number generator to initialise
seed: The seed value to initialise this random number generator
Example:
import "core:math/rand"
import "core:fmt"
init_example :: proc() {
my_rand: rand.Rand
rand.init(&my_rand, 1)
fmt.println(rand.uint64(&my_rand))
}
Possible Output:10
init_as_system ¶
init_as_system :: proc(r: ^Rand = nil) {…}
Initialises a random number generator to use the system random number generator.
The system random number generator is platform specific.
On linux
refer to the getrandom
syscall.
On darwin
refer to getentropy
.
On windows
refer to BCryptGenRandom
.
All other platforms are not supported
Inputs:
r: The random number generator to use the system random number generator
WARNING: Panics if the system is not either windows
, darwin
or linux
Example:
import "core:math/rand"
import "core:fmt"
init_as_system_example :: proc() {
my_rand: rand.Rand
rand.init_as_system(&my_rand)
fmt.println(rand.uint64(&my_rand))
}
Possible Output:10
int127 ¶
Generates 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.
Inputs:
r: The random number generator to use, or nil for the global generator
Returns:
val: A random 127 bit value
Example:
import "core:math/rand"
import "core:fmt"
int127_example :: proc() {
// Using the global random number generator
fmt.println(rand.int127())
// Using local random number generator
my_rand := rand.create(1)
fmt.println(rand.int127(&my_rand))
}
Possible Output:10 389
int127_max ¶
Generates 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
r: The random number generator to use, or nil for the global generator
Returns:
val: A random 127 bit value in the range [0, n)
WARNING: Panics if n is less than 0
Example:
import "core:math/rand"
import "core:fmt"
int127_max_example :: proc() {
// Using the global random number generator
fmt.println(rand.int127_max(16))
// Using local random number generator
my_rand := rand.create(1)
fmt.println(rand.int127_max(1024, &my_rand))
}
Possible Output:6 500
int31 ¶
Generates 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.
Inputs:
r: The random number generator to use, or nil for the global generator
Returns:
val: A random 31 bit value
Example:
import "core:math/rand"
import "core:fmt"
int31_example :: proc() {
// Using the global random number generator
fmt.println(rand.int31())
// Using local random number generator
my_rand := rand.create(1)
fmt.println(rand.int31(&my_rand))
}
Possible Output:10 389
int31_max ¶
Generates 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
r: The random number generator to use, or nil for the global generator
Returns:
val: A random 31 bit value in the range [0, n)
WARNING: Panics if n is less than 0
Example:
import "core:math/rand"
import "core:fmt"
int31_max_example :: proc() {
// Using the global random number generator
fmt.println(rand.int31_max(16))
// Using local random number generator
my_rand := rand.create(1)
fmt.println(rand.int31_max(1024, &my_rand))
}
Possible Output:6 500
int63 ¶
Generates 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.
Inputs:
r: The random number generator to use, or nil for the global generator
Returns:
val: A random 63 bit value
Example:
import "core:math/rand"
import "core:fmt"
int63_example :: proc() {
// Using the global random number generator
fmt.println(rand.int63())
// Using local random number generator
my_rand := rand.create(1)
fmt.println(rand.int63(&my_rand))
}
Possible Output:10 389
int63_max ¶
Generates 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
r: The random number generator to use, or nil for the global generator
Returns:
val: A random 63 bit value in the range [0, n)
WARNING: Panics if n is less than 0
Example:
import "core:math/rand"
import "core:fmt"
int63_max_example :: proc() {
// Using the global random number generator
fmt.println(rand.int63_max(16))
// Using local random number generator
my_rand := rand.create(1)
fmt.println(rand.int63_max(1024, &my_rand))
}
Possible Output:6 500
int_max ¶
Generates 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
r: The random number generator to use, or nil for the global generator
Returns:
val: A random integer value in the range [0, n)
WARNING: Panics if n is less than 0
Example:
import "core:math/rand"
import "core:fmt"
int_max_example :: proc() {
// Using the global random number generator
fmt.println(rand.int_max(16))
// Using local random number generator
my_rand := rand.create(1)
fmt.println(rand.int_max(1024, &my_rand))
}
Possible Output:6 500
norm_float64 ¶
norm_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]
perm ¶
perm :: proc(n: int, r: ^Rand = nil, allocator := context.allocator) -> (res: []int, err: runtime.Allocator_Error) #optional_ok {…}
Creates 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
r: The random number generator to use, or nil for the global generator
allocator: (default: context.allocator)
Returns:
res: A slice filled with random values
err: An allocator error if one occured, nil
otherwise
Example:
import "core:math/rand"
import "core:mem"
import "core:fmt"
perm_example :: proc() -> (err: mem.Allocator_Error) {
// Using the global random number generator and using the context allocator
data := rand.perm(4) or_return
fmt.println(data)
defer delete(data, context.allocator)
// Using local random number generator and temp allocator
my_rand := rand.create(1)
data_tmp := rand.perm(4, &my_rand, context.temp_allocator) or_return
fmt.println(data_tmp)
return
}
Possible Output:[7201011, 3, 9123, 231131] [19578, 910081, 131, 7]
read ¶
Fills 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
r: The random number generator to use, or nil for the global generator
Returns:
n: The number of bytes generated
Example:
import "core:math/rand"
import "core:fmt"
read_example :: proc() {
// Using the global random number generator
data: [8]byte
n := rand.read(data[:])
fmt.println(n)
fmt.println(data)
}
Possible Output:8 [32, 4, 59, 7, 1, 2, 2, 119]
set_global_seed ¶
set_global_seed :: proc(seed: u64) {…}
Sets the seed used by the global random number generator.
Inputs:
seed: The seed value
Example:
import "core:math/rand"
import "core:fmt"
set_global_seed_example :: proc() {
rand.set_global_seed(1)
fmt.println(rand.uint64())
}
Possible Output:10
shuffle ¶
shuffle :: proc(array: $T/[]$T, r: ^Rand = nil) {…}
Randomizes 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
r: The random number generator to use, or nil for the global generator
Example:
import "core:math/rand"
import "core:fmt"
shuffle_example :: proc() {
// Using the global random number generator
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 ¶
Generates a random 128 bit value using the provided random number generator. If no generator is provided the global random number generator will be used.
Inputs:
r: The random number generator to use, or nil for the global generator
Returns:
val: A random unsigned 128 bit value
Example:
import "core:math/rand"
import "core:fmt"
uint128_example :: proc() {
// Using the global random number generator
fmt.println(rand.uint128())
// Using local random number generator
my_rand := rand.create(1)
fmt.println(rand.uint128(&my_rand))
}
Possible Output:10 389
uint32 ¶
Generates a random 32 bit value using the provided random number generator. If no generator is provided the global random number generator will be used.
Inputs:
r: The random number generator to use, or nil for the global generator
Returns:
val: A random unsigned 32 bit value
Example:
import "core:math/rand"
import "core:fmt"
uint32_example :: proc() {
// Using the global random number generator
fmt.println(rand.uint32())
// Using local random number generator
my_rand := rand.create(1)
fmt.println(rand.uint32(&my_rand))
}
Possible Output:10 389
uint64 ¶
Generates a random 64 bit value using the provided random number generator. If no generator is provided the global random number generator will be used.
Inputs:
r: The random number generator to use, or nil for the global generator
Returns:
val: A random unsigned 64 bit value
Example:
import "core:math/rand"
import "core:fmt"
uint64_example :: proc() {
// Using the global random number generator
fmt.println(rand.uint64())
// Using local random number generator
my_rand := rand.create(1)
fmt.println(rand.uint64(&my_rand))
}
Possible Output:10 389
Procedure Groups
This section is empty.
Source Files
- distributions.odin
- exp.odin
- normal.odin
- rand.odin
- (hidden platform specific files)
Generation Information
Generated with odin version dev-2024-04 (vendor "odin") Windows_amd64 @ 2024-04-26 21:08:58.589577200 +0000 UTC