random

extras/random.vhdl

extras_2008/random_20xx.vhdl

Dependencies

timing_ops

Description

This package provides a general set of pseudo-random number functions. It is implemented as a wrapper around the ieee.math_real.uniform procedure and is only suitable for simulation not synthesis. See the LCAR and LFSR packages for synthesizable random generators.

This package makes use of shared variables to keep track of the PRNG state more conveniently than calling uniform directly. Because VHDL-2002 broke forward compatability of shared variables there are two versions of this package. One (random.vhdl) is for VHDL-93 using the classic shared variable mechanism. The other (random_20xx.vhdl) is for VHDL-2002 and later using a protected type to manage the PRNG state. Both files define a package named “random” and only one can be in use at any time. The user visible subprograms are the same in both implementations.

The package provides a number of overloaded subprograms for generating random numbers of various types.

Example usage

seed(12345);    -- Initialize PRNG with a seed value
seed(123, 456); -- Alternate seed procedure

variable : r : real    := random; -- Generate a random real
variable : n : natural := random; -- Generate a random natural
variable : b : boolean := random; -- Generate a random boolean
-- Generate a random bit_vector of any size
variable : bv : bit_vector(99 downto 0) := random(100);

-- Generate a random integer within a specified range
-- Number between 2 and 10 inclusive
variable : i : natural := randint(2, 10);

Subprograms

random.seed (S : in positive)
Seed the PRNG with a number.
Parameters:
  • S (in positive) – Seed value
random.seed (S1 : in positive; S2 : in positive)
Seed the PRNG with s1 and s2. This offers more random initialization than the one argument version of seed.
Parameters:
  • S1 (in positive) – Seed value 1
  • S2 (in positive) – Seed value 2
random.random() → real
Generate a random real.
Returns:Random value.
random.random() → natural
Generate a random natural.
Returns:Random value.
random.random() → boolean
Generate a random boolean.
Returns:Random value.
random.random() → character
Generate a random character.
Returns:Random value.
random.random (Size : positive) → bit_vector
Generate a random bit_vector of size bits.
Parameters:
  • Size (positive) – Length of the random result
Returns:

Random value.

random.randint (Min : integer; Max : integer) → integer
Generate a random integer between Min and Max inclusive. Note that the span Max - Min must be less than integer’high.
Parameters:
  • Min (integer) – Minimum value
  • Max (integer) – Maximum value
Returns:

Random value between Min and Max.

random.randtime (Min : time; Max : time) → time
Generate a random time between Min and Max inclusive. Note that the span Max - Min must be less than time’high.
Parameters:
  • Min (time) – Minimum value
  • Max (time) – Maximum value
Returns:

Random value between Min and Max.