std.rand.Random
class pub Random
A cryptographically secure pseudo random number generator (CSPRNG).
The algorithm used is unspecified but guaranteed to be cryptographically secure.
Static methods
from_int
Show source codeHide source code
fn pub static from_int(seed: Int) -> Random {
Random(inko_random_from_int(seed))
}
fn pub static from_int(seed: Int) -> Random
Returns a new Random
using the given Int
as its seed.
Random
instances created using this method are not suitable for
cryptography, as a single Int
doesn't produce enough entropy. For
cryptography you must use Random.new
instead.
Examples
import std.rand (Random)
Random.from_int(42)
new
Show source codeHide source code
fn pub static new -> Random {
Random(inko_random_new(_INKO.process))
}
fn pub static new -> Random
Returns a new Random
seeded using a cryptographically secure seed.
Seeding is performed by the runtime using a thread-local random number generator suitable for cryptography.
Examples
import std.rand (Random)
Random.new
Instance methods
bytes
Show source codeHide source code
fn pub bytes(size: Int) -> ByteArray {
inko_random_bytes(_INKO.state, _INKO.process, @rng, size)
}
fn pub bytes(size: Int) -> ByteArray
Returns a ByteArray
containing randomly generated bytes.
The returned ByteArray
will contain exactly size
bytes.
Panics
This method might panic if no random bytes could be generated.
float
Show source codeHide source code
fn pub mut float -> Float {
inko_random_float(@rng) as Float
}
fn pub mut float -> Float
Returns a randomly generated Float
.
Examples
import std.rand (Random)
Random.new.float
float_between
Show source codeHide source code
fn pub float_between(min: Float, max: Float) -> Float {
inko_random_float_range(@rng, min as Float64, max as Float64) as Float
}
fn pub float_between(min: Float, max: Float) -> Float
Returns a randomly generated Float
in the given range.
The returned value is in the range start <= value < stop
. If
start >= stop
is true, this method returns 0.0
.
int
Show source codeHide source code
fn pub mut int -> Int {
inko_random_int(@rng) as Int
}
fn pub mut int -> Int
Returns a randomly generated Int
.
Examples
import std.rand (Random)
Random.new.int
int_between
Show source codeHide source code
fn pub int_between(min: Int, max: Int) -> Int {
inko_random_int_range(@rng, min, max) as Int
}
fn pub int_between(min: Int, max: Int) -> Int
Returns a randomly generated Int
in the given range.
The returned value is in the range start <= value < stop
. If
start >= stop
is true, this method returns 0
.
Implemented traits
Drop
impl Drop for Random