DEVELOPMENT EDITION · 769442725406
Library Reference
On this page

Ada.Numerics.Discrete_Random

Uniformly distributed values of a discrete subtype from a generator the program declares and passes (RM A.5.2).

Read the family guide for representation, selection and resource rules. The complete declaration page retains exact signatures/contracts; the source specification is the implementation baseline.

Availability and ownership

A generic over a discrete subtype, with the generator of Ada.Numerics.Float_Random; a value is the next 64-bit word modulo the range's count. Random is a function with side effects on its in out generator. A result is known only to lie in its range. Its bodies are proved in instances (test/programs/prove/library-ada-numerics-random).

A client proof uses these contracts without proving the body again (see library assurance). Global => null excludes package-global effects, not changes made through an in out argument.

Import and instantiate

with Ada.Numerics.Discrete_Random;
import Ada.Numerics.Discrete_Random;

A Vibe module imports this unit's names with from Ada.Numerics.Discrete_Random import Name; see library use across syntaxes. In Varc, keep each name's declared spelling and write & on arguments the call changes.

Contract and lifecycle reading

A Pre condition is the caller's obligation; a Post is what a normal return guarantees. What a postcondition does not state, a client cannot assume. Ghost functions are for contracts and ghost code. The family guide describes copying, lifetime, order and cost.

API 001

Result_Subtype (declaration) — Ada.Numerics.Discrete_Random (RM A.5.2): uniformly distributed values of a discrete subtype from a generator the program declares and passes. A generator's state is an object of its own, changed by each call (Random is a function with side effects on Gen), so no state is hidden from the proof; a result is known only to lie in its range. The generator is xoshiro256** (Blackman and Vigna, 2018), seeded through SplitMix64: a state of four 64-bit words, period 2 ** 256 - 1. It is not for cryptography.

Declared interface:

generic
   type Result_Subtype is (<>);

API 002

Generator (declaration) — Ada.Numerics.Discrete_Random (RM A.5.2): uniformly distributed values of a discrete subtype from a generator the program declares and passes. A generator's state is an object of its own, changed by each call (Random is a function with side effects on Gen), so no state is hidden from the proof; a result is known only to lie in its range. The generator is xoshiro256** (Blackman and Vigna, 2018), seeded through SplitMix64: a state of four 64-bit words, period 2 ** 256 - 1. It is not for cryptography.

Declared interface:

type Generator is limited private;

API 003

Random (declaration) — The next value: the next 64-bit word modulo the subtype's number of values (so each value comes with a probability within Count / 2 ** 64 of 1 / Count)

Declared interface:

function Random (Gen : in out Generator) return Result_Subtype
  with Side_Effects, Global => null,
       Pre => Result_Subtype'First <= Result_Subtype'Last;

API 004

Random (declaration) — The next value from First .. Last, likewise.

Declared interface:

function Random (Gen : in out Generator; First, Last : Result_Subtype) return Result_Subtype
  with Side_Effects, Global => null,
       Pre => First <= Last,
       Post => Random'Result in First .. Last;

API 005

Reset (declaration) — The state the initiator chooses: the same initiator gives the same values.

Declared interface:

procedure Reset (Gen : in out Generator; Initiator : Integer)
  with Global => null;

API 006

Reset (declaration) — A state chosen by the time (the monotonic clock's nanoseconds)

Declared interface:

procedure Reset (Gen : in out Generator)
  with Global => null;

API 007

State (declaration) — A state chosen by the time (the monotonic clock's nanoseconds)

Declared interface:

type State is private;

API 008

Save (declaration) — A state chosen by the time (the monotonic clock's nanoseconds)

Declared interface:

procedure Save (Gen : Generator; To_State : out State)
  with Global => null;

API 009

Reset (declaration) — A state chosen by the time (the monotonic clock's nanoseconds)

Declared interface:

procedure Reset (Gen : in out Generator; From_State : State)
  with Global => null;

API 010

Max_Image_Width (declaration) — A state's image: its four words in hexadecimal, 16 digits each.

Declared interface:

Max_Image_Width : constant := 64;

API 011

Is_Image (declaration) — (Verada) whether a string is the image of a state: Max_Image_Width hexadecimal digits, of either case.

Declared interface:

function Is_Image (Coded_State : String) return Boolean is
  (Coded_State'Length = Max_Image_Width
   and then (for all C of Coded_State => C in '0' .. '9' | 'A' .. 'F' | 'a' .. 'f'));

API 012

Image (declaration) — (Verada) whether a string is the image of a state: Max_Image_Width hexadecimal digits, of either case.

Declared interface:

function Image (Of_State : State) return String
  with Global => null,
       Post => Image'Result'First = 1 and then Is_Image (Image'Result);

API 013

Value (declaration) — The state an image names (where the RM raises Constraint_Error, the precondition requires an image; the image of no state, all zeros, gives Reset (Gen, 0)'s state)

Declared interface:

function Value (Coded_State : String) return State
  with Global => null,
       Pre => Is_Image (Coded_State);