Random

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

An instance of each class that includes Random is a random number generator with its own state- Usually such RNGs can be initialized with a seed, which defines their initial state- It is guaranteed that after initializing two different instances with the same seed, and then executing the same calls on both of them, you will get the same results- This allows exactly reproducing the same seemingly random events by just keeping the seed-

It is possible to make a custom RNG by including Random and implementing #next_u to return an unsigned number of a pre-determined type (usually UInt32). The numbers generated by your RNG must be uniformly distributed in the whole range of possible values for that type (e.g. 0u32..UInt32::MAX). This allows all other methods of Random to be based on this and still produce uniformly distributed results- Your Random class should also have a way to initialize it. For pseudo-random number generators that will usually be an integer seed, but there are no rigid requirements for the initialization.

Direct including types

Defined in:

random/mt19937.cr
random.cr

Constant Summary

DEFAULT = MT19937.new

Class Method Summary

Instance Method Summary

Class Method Detail

def self.new(seed = new_seed)Source

Initializes an instance with the given seed. (Default: #new_seed)

def self.new_seed : UInt32Source

Returns a UInt32 read from a counter value generated by the cycle counter register, or the current time on ARM processors.

def self.rand(x)Source

def self.rand : Float64Source

See #rand.

Instance Method Detail

def next_bool : BoolSource

Generates a random Bool-

Random.new.next_bool # => true

def next_float : Float64Source

See #rand.

def next_int : Int32Source

abstract def next_u : UIntSource

Generates a random unsigned integer.

The integers must be uniformly distributed between 0 and the maximal value for the chosen type.

def rand(max : Int) : IntSource

Generates a random integer which is greater than or equal to 0 and less than max.

The return type always matches the supplied argument.

Random.new.rand(10)   # => 5
Random.new.rand(5000) # => 2243

def rand(max : Float) : Float64Source

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

def rand(range : Range(Int, Int)) : IntSource

Returns a random integer in the given range.

The return type always matches the supplied argument.

Random.new.rand(10..20)                 # => 14
Random.new.rand(Int64::MIN..Int64::MAX) # => -5297435808626736140

def rand(range : Range(Float, Float)) : Float64Source

Returns a random Float64 in the given range.

Random.new.rand(6.2..21.768) # => 15.2989

def rand : Float64Source

Generates a random Float64 between 0 and 1.

r = Random.new
r.rand # => 0.167595
r.rand # => 0.0372991

© 2012–2017 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/0.22.0/Random.html

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部