module Random
Overview
Random
provides an interface for random values generation, using a pseudo random number generator (PRNG).
Random.new_seed # => 112705036
Random.rand # => 0.167595
Random.rand(5) # => 2
The above methods delegate to a Random
instance.
r = Random.new
r.rand # => 0.0372991
r.next_bool # => true
r.next_int # => 2223112
This module also defines a global method #rand
, which Array#sample
and Array#shuffle
delegates.
rand # => 0.293829
rand(10) # => 8
Direct including types
Defined in:
random/mt19937.crrandom.cr
Constant Summary
-
DEFAULT =
MT19937.new
Class Method Summary
-
.new(seed = new_seed)
Initiates an instance with the given seed.
-
.new_seed : UInt32
Returns a
UInt32
read from a counter value generated by the cycle counter register. -
.rand : Float64
see
#rand
-
.rand(x)
see
#rand(x)
Instance Method Summary
-
#next_bool : Bool
Generates a random
Bool
. -
#next_float : Float64
see
#rand
-
#next_int : Int32
Generates a random
Int32
. -
#next_u32
Generates a random
UInt32
. -
#rand(range : Range(Float, Float)) : Float64
Returns a random
Float64
in the given range. -
#rand : Float64
Generates a random
Float64
between 0 and 1. -
#rand(range : Range(Int, Int)) : Int32
Returns a random
Int32
in the given range. -
#rand(max : Int) : Int32
Returns a random
Int32
which is greater than or equal to 0 and less than max. -
#rand(max : Float) : Float64
Returns a random
Float64
which is greater than or equal to 0 and less than max.
Class Method Detail
Returns a UInt32
read from a counter value
generated by the cycle counter register.
Instance Method Detail
Generates a random Int32
.
Random.new.next_int # => 440038499
Random.new.next_int # => -1848788484
Returns a random Float64
in the given range.
Random.new.rand(6.2..21.768) # => 15.2989
Generates a random Float64
between 0 and 1.
r = Random.new
r.rand # => 0.167595
r.rand # => 0.0372991
Returns a random Int32
in the given range.
Random.new.rand(10..20) # => 14
Returns a random Int32
which is greater than or equal to 0 and less than max.
Random.new.rand(10) # => 5
Random.new.rand(5000) # => 2243
Returns a random Float64
which is greater than or equal to 0 and less than max.
Random.new.rand(3.5) # => 2.88938
Random.new.rand(10.725) # => 7.70147