- README

- Top Level Namespace
- Adler32
- ArgumentError
- Array
- Atomic
- Base64
  - Error
- Benchmark
  - BM
    - Job
    - Tms
  - IPS
    - Entry
    - Job
- BigFloat
- BigInt
- BigRational
- BitArray
- Bool
- Box
- Bytes
- Channel
  - Buffered
  - ClosedError
  - ReceiveAction
  - SelectAction
  - SendAction
  - Unbuffered
- Char
  - Reader
- Class
- Colorize
  - Object
  - ObjectExtensions
- Comparable
- Complex
- Concurrent
  - CanceledError
- CRC32
- Crypto
  - Bcrypt
    - Error
    - Password
  - Blowfish
  - Subtle
- CSV
  - Builder
    - Row
  - Error
  - Lexer
  - MalformedCSVError
  - Parser
  - Row
  - Token
    - Kind
- Debug
  - DWARF
    - Abbrev
      - Attribute
    - AT
    - FORM
    - Info
      - Value
    - LineNumbers
      - Register
      - Row
      - Sequence
    - LNE
    - LNS
    - Strings
    - TAG
  - ELF
    - Endianness
    - Error
    - Ident
    - Klass
    - Machine
    - OSABI
    - SectionHeader
      - Flags
      - Type
    - Type
- Deque
- Digest
  - Base
  - MD5
  - SHA1
- Dir
- DivisionByZero
- DL
- ECR
- Enum
- Enumerable
  - EmptyError
- ENV
- Errno
- Exception
- Fiber
- File
  - Stat
- FileUtils
- Flate
  - Error
  - Reader
  - Strategy
  - Writer
- Float
  - Primitive
- Float32
- Float64
- GC
  - Stats
- Gzip
  - Error
  - Header
  - Reader
  - Writer
- Hash
- HTML
- HTTP
  - Client
    - BodyType
    - Response
  - CompressHandler
  - ComputedContentTypeHeader
  - Cookie
  - Cookies
  - ErrorHandler
  - FormData
    - Builder
    - Error
    - FileMetadata
    - Parser
    - Part
  - Handler
    - Proc
  - Headers
  - LogHandler
  - Multipart
    - Builder
    - Error
    - Parser
  - Params
    - Builder
  - Request
  - Server
    - Context
    - RequestProcessor
    - Response
  - StaticFileHandler
    - DirectoryListing
  - WebSocket
  - WebSocketHandler
- Indexable
- IndexError
- INI
- Int
  - Primitive
  - Signed
  - Unsigned
- Int16
- Int32
- Int64
- Int8
- InvalidByteSequenceError
- IO
  - Buffered
  - ByteFormat
    - BigEndian
    - LittleEndian
    - NetworkEndian
    - SystemEndian
  - Delimited
  - EncodingOptions
  - EOFError
  - Error
  - FileDescriptor
  - Hexdump
  - Memory
  - MultiWriter
  - Seek
  - Sized
  - Timeout
- IPSocket
- Iterable
- Iterator
  - IteratorWrapper
  - Stop
- JSON
  - Any
  - Builder
    - ArrayState
    - DocumentEndState
    - DocumentStartState
    - ObjectState
    - StartState
    - State
  - Error
  - Lexer
  - ParseException
  - Parser
  - PullParser
  - Token
  - Type
- KeyError
- Levenshtein
  - Finder
- LLVM
  - ABI
    - AArch64
    - ArgKind
    - ArgType
    - ARM
    - FunctionType
    - X86
    - X86_64
      - RegClass
  - AtomicOrdering
  - AtomicRMWBinOp
  - Attribute
  - AttributeIndex
  - BasicBlock
  - BasicBlockCollection
  - Builder
  - CallConvention
  - CodeGenFileType
  - CodeGenOptLevel
  - CodeModel
  - Context
  - DIBuilder
  - DIFlags
  - DwarfTag
  - DwarfTypeEncoding
  - Function
  - FunctionCollection
  - FunctionPassManager
    - Runner
  - GenericValue
  - GlobalCollection
  - InstructionCollection
  - IntPredicate
  - JITCompiler
  - Linkage
  - MemoryBuffer
  - Module
  - ModuleFlag
  - ModulePassManager
  - ParameterCollection
  - PassManagerBuilder
  - PassRegistry
  - PhiTable
  - RealPredicate
  - RelocMode
  - Target
  - TargetData
  - TargetMachine
  - Type
    - Kind
  - Value
    - Kind
  - ValueMethods
  - VerifierFailureAction
- Logger
  - Formatter
  - Severity
- Markdown
  - HTMLRenderer
  - Parser
    - PrefixHeader
    - UnorderedList
  - Renderer
- Math
- Mutex
- NamedTuple
- Nil
- Number
  - Primitive
- OAuth
  - AccessToken
  - Consumer
  - Error
  - RequestToken
- OAuth2
  - AccessToken
    - Bearer
    - Mac
  - Client
  - Error
  - Session
- Object
- OpenSSL
  - Digest
    - Error
    - UnsupportedError
  - DigestBase
  - Error
  - HMAC
  - MD5
  - SHA1
  - SSL
    - Context
      - Client
      - Server
    - Error
    - ErrorType
    - Modes
    - Options
    - Socket
      - Client
      - Server
    - VerifyMode
    - X509VerifyFlags
- OptionParser
  - Exception
  - InvalidOption
  - MissingOption
- PartialComparable
- Pointer
  - Appender
- PrettyPrint
- Proc
- Process
  - Env
  - Status
  - Stdio
  - Tms
- Random
  - MT19937
- Range
- Readline
  - CompletionProc
  - KeyBindingProc
- Reference
- Reflect
- Regex
  - MatchData
  - Options
- SecureRandom
- Set
- Signal
- Slice
- Socket
  - Address
  - Addrinfo
  - Error
  - Family
  - IPAddress
  - Protocol
  - Server
  - Type
  - UNIXAddress
- Spec
  - Expectations
  - Methods
  - ObjectExtensions
- StaticArray
- String
  - Builder
  - RawConverter
- StringPool
- StringScanner
- Struct
- Symbol
- System
- TCPServer
- TCPSocket
- Tempfile
- Termios
  - AttributeSelection
  - BaudRate
  - ControlMode
  - InputMode
  - LineControl
  - LocalMode
  - OutputMode
- Time
  - DayOfWeek
  - EpochConverter
  - EpochMillisConverter
  - Format
    - Error
  - Kind
  - MonthSpan
  - Span
- Tuple
- TypeCastError
- UDPSocket
- UInt16
- UInt32
- UInt64
- UInt8
- Unicode
  - CaseOptions
- Union
- UNIXServer
- UNIXSocket
- URI
  - Error
- Value
- WeakRef
- XML
  - Attributes
  - AttributeType
  - Builder
  - Error
  - HTMLParserOptions
  - Namespace
  - Node
  - NodeSet
  - ParserOptions
  - Reader
  - SaveOptions
  - Type
  - XPathContext
- YAML
  - Any
  - Builder
  - Error
  - EventKind
  - ParseException
  - Parser
  - PullParser
  - Type
- Zip
  - CompressionMethod
  - Error
  - File
    - Entry
  - FileInfo
  - Reader
    - Entry
  - Writer
    - Entry
- Zlib
  - Error
  - Reader
  - Writer

# 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

- Random::MT19937

## Defined in:

## Constant Summary

DEFAULT = `MT19937.new`

## Class Method Summary

- .new(seed = new_seed)

  Initializes an instance with the given seed.

- .new_seed : UInt32

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

- .rand(x)

  See `#rand(x)`.

- .rand : Float64

  See `#rand`.

## Instance Method Summary

- #next_bool : Bool

  Generates a random `Bool`.

- #next_float : Float64

  See `#rand`.

- #next_int : Int32

  Same as `#rand(Int32::MIN..Int32::MAX)`.

- #next_u : UInt

  Generates a random unsigned integer.

- #rand(max : Int) : Int

  Generates a random integer 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.

- #rand(range : Range(Int, Int)) : Int

  Returns a random integer in the given range.

- #rand(range : Range(Float, Float)) : Float64

  Returns a random `Float64` in the given range.

- #rand : Float64

  Generates a random `Float64` between `0` and `1`.

## Class Method Detail

def self.new(seed = new_seed) #

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

def self.new_seed : UInt32 #

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) #

See `#rand(x)`.

def self.rand : Float64 #

See `#rand`.

## Instance Method Detail

def next_bool : Bool #

Generates a random `Bool`.

    Random.new.next_bool # => true

def next_float : Float64 #

See `#rand`.

def next_int : Int32 #

Same as `#rand(Int32::MIN..Int32::MAX)`.

abstract def next_u : UInt #

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) : Int #

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) : Float64 #

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)) : Int #

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)) : Float64 #

Returns a random `Float64` in the given range.

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

def rand : Float64 #

Generates a random `Float64` between `0` and `1`.

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