Alea::Random(G)
Inherits Struct < Value < Object
Alea::Random provides the interface for pseudo-random generations and distribution sampling.
NOTE: due to this, you still need to specify the generator as the generic type, as it does not accept a default one.
seed = 9377
random = Alea::Random(Alea::XSR128).new(seed)
random.float # => 0.08153691876972058
# Passing an instance of a PRNG:
xsr = Alea::XSR256.new 9377, 2353
random = Alea::Random.new xsr
random.prng # => Alea::XSR256
As long as it includes the Alea::PRNG module, you can build your own generator: check
out the example provided in
the documentation to make it a valid extension for Alea::Random.
The following implementations are taken from numpy.
Constructors
Initializes the PRNG with initial seeds.
@parameters:
seed32: value as input to init. the state of 32-bit generators ofprng.seed64: value as input to init. the state of 64-bit generators ofprng.
@exceptions:
Alea::UndefinedErrorif any ofseed32orseed64is negative.
Initializes the PRNG with initial seed.
@parameters:
seed: initial seed as input for generating the state ofprng.
@exceptions:
Alea::UndefinedErrorifseedis negative.
Instance methods
Generate a beta-distributed, pseudo-random Float64 in range [0, 1).
@note: named arguments are mandatory to prevent ambiguity.
@parameters:
a: shape parameter of the distribution; usually mentioned asα.b: shape parameter of the distribution; usually mentioned asβ.
@exceptions:
Alea::NaNErrorif any of the arguments isNaN.Alea::InfinityErrorif any of the arguments isInfinity.Alea::UndefinedErrorif any ofaorbis negative or zero.
Generate a beta-distributed, pseudo-random Float32 in range [0, 1).
@note: named arguments are mandatory to prevent ambiguity.
@parameters:
a: shape parameter of the distribution; usually mentioned asα.b: shape parameter of the distribution; usually mentioned asβ.
@exceptions:
Alea::NaNErrorif any of the arguments isNaN.Alea::InfinityErrorif any of the arguments isInfinity.Alea::UndefinedErrorif any ofaorbis negative or zero.
Generate a chi-square-distributed, pseudo-random Float64.
@parameters:
df: degrees of freedom of the distribution; usually mentioned ask.
@exceptions:
Alea::NaNErrorif any of the arguments isNaN.Alea::InfinityErrorif any of the arguments isInfinity.Alea::UndefinedErrorifdfis negative or zero.
Generate a chi-square-distributed, pseudo-random Float32.
@parameters:
df: degrees of freedom of the distribution; usually mentioned ask.
@exceptions:
Alea::NaNErrorif any of the arguments isNaN.Alea::InfinityErrorif any of the arguments isInfinity.Alea::UndefinedErrorifdfis negative or zero.
Generate a exp-distributed, pseudo-random Float64.
@parameters:
scale: scale parameter of the distribution; usually mentioned asλ^-1.
@exceptions:
Alea::NaNErrorif any of the arguments isNaN.Alea::InfinityErrorif any of the arguments isInfinity.Alea::UndefinedErrorifscaleis negative or zero.
Generate a exp-distributed, pseudo-random Float32.
@parameters:
scale: scale parameter of the distribution; usually mentioned asλ^-1.
@exceptions:
Alea::NaNErrorif any of the arguments isNaN.Alea::InfinityErrorif any of the arguments isInfinity.Alea::UndefinedErrorifscaleis negative or zero.
Generate a f-snedecor-distributed, pseudo-random Float64.
@parameters:
df1: degrees of freedom of the underlying chi-square distribution, numerator side; usually mentioned asm.df2: degrees of freedom of the underlying chi-square distribution, denominator side; usually mentioned asn.
@note: due to homonymy with float methods, the unparsed method
is indentified with next_fs.
@exceptions:
Alea::NaNErrorif any of the arguments isNaN.Alea::InfinityErrorif any of the arguments isInfinity.Alea::UndefinedErrorifdfis negative or zero.
Generate a f-snedecor-distributed, pseudo-random Float32.
@parameters:
df1: degrees of freedom of the underlying chi-square distribution, numerator side; usually mentioned asm.df2: degrees of freedom of the underlying chi-square distribution, denominator side; usually mentioned asn.
@note: due to homonymy with float methods, the unparsed method
is indentified with next_fs32.
@exceptions:
Alea::NaNErrorif any of the arguments isNaN.Alea::InfinityErrorif any of the arguments isInfinity.Alea::UndefinedErrorifdfis negative or zero.
Generate a uniform-distributed, pseudo-random Float64 in fixed range.
@parameters:
min: left bound parameter of range of the distribution; usually mentioned asa.max: right bound parameter of range of the distribution; usually mentioned asb.
Generate a uniform-distributed, pseudo-random Float64 in range [0.0, max).
@parameters:
max: right bound parameter of range of the distribution; usually mentioned asb.
@exceptions:
Alea::NaNErrorif any of the arguments isNaN.Alea::InfinityErrorif any of the arguments isInfinity.Alea::UndefinedErrorifmaxis negative or zero.
Generate a uniform-distributed, pseudo-random Float64 in fixed range.
@parameters:
range: range parameter, inclusive or exclusive, of the distribution:range.begin: left bound parameter of range of the distribution; usually mentioned asa.range.end: right bound parameter of range of the distribution; usually mentioned asb.
@notes:
- inclusive means
[range.begin, range.end]. - exclusive means
[range.begin, range.end). - see
Rangefrom Crystal stdlib.
@examples:
range_in = 10.0..9377.0
range_in # Range(Float64, Float64), end-inclusive
range_ex = 10.0...9377.0
range_ex # Range(Float64, Float64), end-exclusive
random = Alea::Random.new
random.float(range_in) # => 9113.861259040154
random.float(range_ex) # => 7701.2778313581175
@exceptions:
Alea::NaNErrorif any of the arguments bound isNaN.Alea::InfinityErrorif any of the arguments bound isInfinity.Alea::UndefinedErrorifrange.endis less thanrange.begin.Alea::UndefinedErrorifrangeis not end-inclusive but bounds are the same.
Generate a uniform-distributed, pseudo-random Float64 in range [0.0, 1.0).
@references: #next_f64.
Generate a uniform-distributed, pseudo-random Float32 in fixed range.
@parameters:
min: left bound parameter of range of the distribution; usually mentioned asa.max: right bound parameter of range of the distribution; usually mentioned asb.
Generate a uniform-distributed, pseudo-random Float32 in range [0.0, max).
@parameters:
max: right bound parameter of range of the distribution; usually mentioned asb.
@exceptions:
Alea::NaNErrorif any of the arguments isNaN.Alea::InfinityErrorif any of the arguments isInfinity.Alea::UndefinedErrorifmaxis negative or zero.
Generate a uniform-distributed, pseudo-random Float64 in fixed range.
@parameters:
range: range parameter, inclusive or exclusive, of the distribution:range.begin: left bound parameter of range of the distribution; usually mentioned asa.range.end: right bound parameter of range of the distribution; usually mentioned asb.
@notes:
- inclusive means
[range.begin, range.end]. - exclusive means
[range.begin, range.end). - see
Rangefrom Crystal stdlib.
@examples:
range_in = 10.0..9377.0
range_in # Range(Float64, Float64), end-inclusive
range_ex = 10.0...9377.0
range_ex # Range(Float64, Float64), end-exclusive
random = Alea::Random.new
random.float32(range_in) # => 950.3449
random.float32(range_ex) # => 3455.0183
@exceptions:
Alea::NaNErrorif any of the arguments bound isNaN.Alea::InfinityErrorif any of the arguments bound isInfinity.Alea::UndefinedErrorifrange.endis less thanrange.begin.Alea::UndefinedErrorifrangeis not end-inclusive but bounds are the same.
Generate a uniform-distributed, pseudo-random Float32 in range [0.0, 1.0).
@references: #next_f32.
Generate a gamma-distributed, pseudo-random Float64.
@parameters:
shape: shape parameter of the distribution; usually mentioned ask.scale: scale parameter of the distribution; usually mentioned asθ.
@exceptions:
Alea::NaNErrorif any of the arguments isNaN.Alea::InfinityErrorif any of the arguments isInfinity.Alea::UndefinedErrorif any ofshapeorscaleis negative or zero.
Generate a gamma-distributed, pseudo-random Float32.
@parameters:
shape: shape parameter of the distribution; usually mentioned ask.scale: scale parameter of the distribution; usually mentioned asθ.
@exceptions:
Alea::NaNErrorif any of the arguments isNaN.Alea::InfinityErrorif any of the arguments isInfinity.Alea::UndefinedErrorif any ofshapeorscaleis negative or zero.
Generate a laplace-distributed, pseudo-random Float64.
@parameters:
loc: centrality parameter, or mean of the distribution; usually mentioned asμ.scale: scale parameter of the distribution; usually mentioned asb.
@exceptions:
Alea::NaNErrorif any of the arguments isNaN.Alea::InfinityErrorif any of the arguments isInfinity.Alea::UndefinedErrorifscaleis negative or zero.
Generate a laplace-distributed, pseudo-random Float32.
@parameters:
loc: centrality parameter, or mean of the distribution; usually mentioned asμ.scale: scale parameter of the distribution; usually mentioned asb.
@exceptions:
Alea::NaNErrorif any of the arguments isNaN.Alea::InfinityErrorif any of the arguments isInfinity.Alea::UndefinedErrorifscaleis negative or zero.
Generate a log-normal-distributed, pseudo-random Float64.
@parameters:
loc: centrality parameter, or mean of the underlying normal distribution; usually mentioned asμ.sigma: scale parameter, or standard deviation of the underlying normal distribution; usually mentioned asσ.
@exceptions:
Alea::NaNErrorif any of the arguments isNaN.Alea::InfinityErrorif any of the arguments isInfinity.Alea::UndefinedErrorifsigmais negative or zero.
Generate a log-normal-distributed, pseudo-random Float32.
@parameters:
loc: centrality parameter, or mean of the underlying normal distribution; usually mentioned asμ.sigma: scale parameter, or standard deviation of the underlying normal distribution; usually mentioned asσ.
@exceptions:
Alea::NaNErrorif any of the arguments isNaN.Alea::InfinityErrorif any of the arguments isInfinity.Alea::UndefinedErrorifsigmais negative or zero.
Generate a beta-distributed, pseudo-random Float64 in range [0, 1).
Unparsed version of #beta.
@note: named arguments are mandatory to prevent ambiguity.
@parameters:
a: shape parameter of the distribution; usually mentioned asα.b: shape parameter of the distribution; usually mentioned asβ.
Generate a beta-distributed, pseudo-random Float32 in range [0, 1).
Unparsed version of #beta32.
@note: named arguments are mandatory to prevent ambiguity.
@parameters:
a: shape parameter of the distribution; usually mentioned asα.b: shape parameter of the distribution; usually mentioned asβ.
Generate a chi-square-distributed, pseudo-random Float64.
Unparsed version of chisq.
@parameters:
df: degrees of freedom of the distribution; usually mentioned ask.
Generate a chi-square-distributed, pseudo-random Float32.
Unparsed version of chisq32.
@parameters:
df: degrees of freedom of the distribution; usually mentioned ask.
Generate a exp-distributed, pseudo-random Float64.
Unparsed version of exp.
@parameters:
scale: scale parameter of the distribution; usually mentioned asλ^-1.
Generate a exp-distributed, pseudo-random Float64.
Unparsed version of exp.
@note:
scaleis1.0.
Generate a exp-distributed, pseudo-random Float32.
Unparsed version of exp32.
@parameters:
scale: scale parameter of the distribution; usually mentioned asλ^-1.
Generate a exp-distributed, pseudo-random Float32.
Unparsed version of exp32.
@note:
scaleis1.0.
Generate a uniform-distributed, pseudo-random Float64 in fixed range.
Unparsed version for #float.
@parameters:
min: left bound parameter of range of the distribution; usually mentioned asa.max: right bound parameter of range of the distribution; usually mentioned asb.
Generate a uniform-distributed, pseudo-random Float64 in range [0, max).
Unparsed version for #float.
@parameters:
max: right bound parameter of range of the distribution; usually mentioned asb.
Generate a uniform-distributed, pseudo-random Float32 in fixed range.
Unparsed version for #float32.
@parameters:
min: left bound parameter of range of the distribution; usually mentioned asa.max: right bound parameter of range of the distribution; usually mentioned asb.
Generate a uniform-distributed, pseudo-random Float32 in range [0, max).
Unparsed version for #float32.
@parameters:
max: right bound parameter of range of the distribution; usually mentioned asb.
Generate a f-snedecor-distributed, pseudo-random Float64.
Unparsed version of #f.
@parameters:
df1: degrees of freedom of the underlying chi-square distribution, numerator side; usually mentioned asm.df2: degrees of freedom of the underlying chi-square distribution, denominator side; usually mentioned asn.
Generate a f-snedecor-distributed, pseudo-random Float32.
Unparsed version of #f32.
@parameters:
df1: degrees of freedom of the underlying chi-square distribution, numerator side; usually mentioned asm.df2: degrees of freedom of the underlying chi-square distribution, denominator side; usually mentioned asn.
Generate a gamma-distributed, pseudo-random Float64.
Unparsed version of #gamma.
@parameters:
shape: shape parameter of the distribution; usually mentioned ask.scale: scale parameter of the distribution; usually mentioned asθ.
Generate a gamma-distributed, pseudo-random Float64.
Unparsed version of #gamma.
@parameters:
shape: shape parameter of the distribution; usually mentioned ask.
@note:
scaleis1.0.
Generate a gamma-distributed, pseudo-random Float32.
Unparsed version of #gamma.
@parameters:
shape: shape parameter of the distribution; usually mentioned ask.scale: scale parameter of the distribution; usually mentioned asθ.
Generate a gamma-distributed, pseudo-random Float32.
Unparsed version of #gamma32.
@parameters:
shape: shape parameter of the distribution; usually mentioned ask.
@note:
scaleis1.0.
Generate a laplace-distributed, pseudo-random Float64.
Unparsed version of laplace.
@parameters:
loc: centrality parameter, or mean of the distribution; usually mentioned asμ.scale: scale parameter of the distribution; usually mentioned asb.
Generate a laplace-distributed, pseudo-random Float64.
Unparsed version of laplace.
@parameters:
loc: centrality parameter, or mean of the distribution; usually mentioned asμ.
@notes:
scaleis1.0.
Generate a laplace-distributed, pseudo-random Float64.
Unparsed version of laplace.
@notes:
locis0.0.scaleis1.0.
Generate a laplace-distributed, pseudo-random Float32.
Unparsed version of #laplace32.
@parameters:
loc: centrality parameter, or mean of the distribution; usually mentioned asμ.scale: scale parameter of the distribution; usually mentioned asb.
Generate a laplace-distributed, pseudo-random Float32.
Unparsed version of #laplace32.
@parameters:
loc: centrality parameter, or mean of the distribution; usually mentioned asμ.
@notes:
scaleis1.0.
Generate a laplace-distributed, pseudo-random Float32.
Unparsed version of #laplace32.
@notes:
locis0.0.scaleis1.0.
Generate a log-normal-distributed, pseudo-random Float64.
Unparsed version of #lognormal.
@parameters:
loc: centrality parameter, or mean of the underlying normal distribution; usually mentioned asμ.sigma: scale parameter, or standard deviation of the underlying normal distribution; usually mentioned asσ.
Generate a log-normal-distributed, pseudo-random Float64.
Unparsed version of #lognormal.
@parameters:
loc: centrality parameter, or mean of the underlying normal distribution; usually mentioned asμ.
@notes:
sigmais1.0.
Generate a log-normal-distributed, pseudo-random Float64.
Unparsed version of #lognormal.
@notes:
locis0.0.sigmais1.0.
Generate a log-normal-distributed, pseudo-random Float32.
Unparsed version of #lognormal32.
@parameters:
loc: centrality parameter, or mean of the underlying normal distribution; usually mentioned asμ.sigma: scale parameter, or standard deviation of the underlying normal distribution; usually mentioned asσ.
Generate a log-normal-distributed, pseudo-random Float32.
Unparsed version of #lognormal32.
@parameters:
loc: centrality parameter, or mean of the underlying normal distribution; usually mentioned asμ.
@notes:
sigmais1.0.
Generate a log-normal-distributed, pseudo-random Float32.
Unparsed version of #lognormal32.
@notes:
locis0.0.sigmais1.0.
Generate a normal-distributed, pseudo-random Float64.
Unparsed version of #normal.
@parameters:
loc: centrality parameter, or mean of the distribution; usually mentioned asμ.sigma: scale parameter, or standard deviation of the distribution; usually mentioned asσ.
Generate a normal-distributed, pseudo-random Float64.
Unparsed version of #normal.
@parameters:
loc: centrality parameter, or mean of the distribution; usually mentioned asμ.
@notes:
sigmais1.0.
Generate a normal-distributed, pseudo-random Float64.
Unparsed version of #normal.
@notes:
locis0.0.sigmais1.0.
Generate a normal-distributed, pseudo-random Float32.
Unparsed version of #normal32.
@parameters:
loc: centrality parameter, or mean of the distribution; usually mentioned asμ.sigma: scale parameter, or standard deviation of the distribution; usually mentioned asσ.
Generate a normal-distributed, pseudo-random Float32.
Unparsed version of #normal32.
@parameters:
loc: centrality parameter, or mean of the distribution; usually mentioned asμ.
@notes:
sigmais1.0.
Generate a normal-distributed, pseudo-random Float32.
Unparsed version of #normal32.
@notes:
locis0.0.sigmais1.0.
Generate a poisson-distributed, pseudo-random Int64.
Unparsed version of #poisson.
@parameters:
lam: separation parameter of the distribution; usually mentioned asλ.
Generate a poisson-distributed, pseudo-random Int64.
Unparsed version of #poisson.
@notes:
lamis1.0.
Generate a t-student-distributed, pseudo-random Float64.
Unparsed version of #t.
@parameters:
df: degrees of freedom of the distribution; usually mentioned asn.
Generate a t-student-distributed, pseudo-random Float32.
Unparsed version of #t32.
@parameters:
df: degrees of freedom of the distribution; usually mentioned asn.
Generate a uniform-distributed, pseudo-random UInt64 in fixed range.
Unparsed version for #uint.
@parameters:
min: left bound parameter of range of the distribution; usually mentioned asa.max: right bound parameter of range of the distribution; usually mentioned asb.
Generate a uniform-distributed, pseudo-random UInt64 in range [0, max).
Unparsed version for #uint.
@parameters:
max: right bound parameter of range of the distribution; usually mentioned asb.
Generate a uniform-distributed, pseudo-random UInt32 in fixed range.
Unparsed version for #uint32.
@parameters:
min: left bound parameter of range of the distribution; usually mentioned asa.max: right bound parameter of range of the distribution; usually mentioned asb.
Generate a uniform-distributed, pseudo-random UInt32 in range [0, max).
Unparsed version for #uint32.
@parameters:
max: right bound parameter of range of the distribution; usually mentioned asb.
Generate a normal-distributed, pseudo-random Float64.
@parameters:
loc: centrality parameter, or mean of the distribution; usually mentioned asμ.sigma: scale parameter, or standard deviation of the distribution; usually mentioned asσ.
@exceptions:
Alea::NaNErrorif any of the arguments isNaN.Alea::InfinityErrorif any of the arguments isInfinity.Alea::UndefinedErrorifsigmais negative or zero.
Generate a normal-distributed, pseudo-random Float32.
@parameters:
loc: centrality parameter, or mean of the distribution; usually mentioned asμ.sigma: scale parameter, or standard deviation of the distribution; usually mentioned asσ.
@exceptions:
Alea::NaNErrorif any of the arguments isNaN.Alea::InfinityErrorif any of the arguments isInfinity.Alea::UndefinedErrorifsigmais negative or zero.
Generate a poisson-distributed, pseudo-random Int64.
@parameters:
lam: separation parameter of the distribution; usually mentioned asλ.
@exceptions:
Alea::NaNErrorif any of the arguments isNaN.Alea::InfinityErrorif any of the arguments isInfinity.Alea::UndefinedErroriflamis negative or zero.
Wrapper around #uint: extends sampling capabilities to Crystal
stdlib Indexable(T) module includers.
rng = Alea::Random(Alea::MT19937).new 27
ary = [6, 3, 2, 9]
tup = {6, 3, 2, 9}
ary.sample rng # => 6
typ.sample rng # => 9
Generate a t-student-distributed, pseudo-random Float64.
@parameters:
df: degrees of freedom of the distribution; usually mentioned asn.
@exceptions:
Alea::NaNErrorif any of the arguments isNaN.Alea::InfinityErrorif any of the arguments isInfinity.Alea::UndefinedErrorifdfis negative or zero.
Generate a t-student-distributed, pseudo-random Float32.
@parameters:
df: degrees of freedom of the distribution; usually mentioned asn.
@exceptions:
Alea::NaNErrorif any of the arguments isNaN.Alea::InfinityErrorif any of the arguments isInfinity.Alea::UndefinedErrorifdfis negative or zero.
Generate a uniform-distributed, pseudo-random UInt64 in fixed range.
@parameters:
min: left bound parameter of range of the distribution; usually mentioned asa.max: right bound parameter of range of the distribution; usually mentioned asb.
Generate a uniform-distributed, pseudo-random UInt64 in range [0, max).
@parameters:
max: right bound parameter of range of the distribution; usually mentioned asb.
@exceptions:
Alea::UndefinedErrorifmaxis negative or zero.
Generate a uniform-distributed, pseudo-random UInt64 in fixed range.
@parameters:
range: range parameter, inclusive or exclusive, of the distribution:range.begin: left bound parameter of range of the distribution; usually mentioned asa.range.end: right bound parameter of range of the distribution; usually mentioned asb.
@notes:
- inclusive means
[range.begin, range.end]. - exclusive means
[range.begin, range.end). - see
Rangefrom Crystal stdlib.
@examples:
range_in = 10..9377
range_in # Range(Int32, Int32), end-inclusive
range_ex = 10...9377
range_ex # Range(Int32, Int32), end-exclusive
random = Alea::Random.new
random.uint(range_in) # => 2640
random.uint(range_ex) # => 527
@exceptions:
Alea::UndefinedErrorifrange.endis less thanrange.begin.Alea::UndefinedErrorifrangeis not end-inclusive but bounds are the same.
Generate a uniform-distributed, pseudo-random UInt32 in fixed range.
@parameters:
min: left bound parameter of range of the distribution; usually mentioned asa.max: right bound parameter of range of the distribution; usually mentioned asb.
Generate a uniform-distributed, pseudo-random UInt32 in range [0, max).
@parameters:
max: right bound parameter of range of the distribution; usually mentioned asb.
@exceptions:
Alea::UndefinedErrorifmaxis negative or zero.
Generate a uniform-distributed, pseudo-random UInt32 in fixed range.
@parameters:
range: range parameter, inclusive or exclusive, of the distribution:range.begin: left bound parameter of range of the distribution; usually mentioned asa.range.end: right bound parameter of range of the distribution; usually mentioned asb.
@notes:
- inclusive means
[range.begin, range.end]. - exclusive means
[range.begin, range.end). - see
Rangefrom Crystal stdlib.
@examples:
range_in = 10..9377
range_in # Range(Int32, Int32), end-inclusive
range_ex = 10...9377
range_ex # Range(Int32, Int32), end-exclusive
random = Alea::Random.new
random.uint32(range_in) # => 9260
random.uint32(range_ex) # => 153
@exceptions:
Alea::UndefinedErrorifrange.endis less thanrange.begin.Alea::UndefinedErrorifrangeis not end-inclusive but bounds are the same.