Random Number Generators

Summary
Random Number Generators
Types
dist_binDefines the structure of a random distribution bin.
random_utilProvides a utility function to generate random numbers.
Functions
random_boolstatic Returns a randomized Boolean value based on the specified percentage.
random_2_bin_numProvides a random number using two distribution bins.
Properties
dbRandom distribution bins.
valrand The randomized value.
Functions
newCreate a random number object.
random_4_bin_numProvides a random number using four distribution bins.
Properties
dbRandom distribution bins.
valrand The randomized value.
Functions
newCreate a random number object.
random_8_bin_numProvides a random number using eight distribution bins.
Properties
dbRandom distribution bins.
valrand The randomized value.
Functions
newCreate a random number object.
random_16_bin_numProvides a random number using sixteen distribution bins.
Properties
dbRandom distribution bins.
valrand The randomized value.
Functions
newCreate a random number object.
random_32_bin_numProvides a random number using thirty-two distribution bins.
Properties
dbRandom distribution bins.
valrand The randomized value.
Functions
newCreate a random number object.
random_power_of_2_numProvides a random number with power-of-two distributions.
Properties
min_valThe minimum limit of val.
max_valThe maximum limit of val.
Functions
newCreate a random number object.
random_power_of_10_numProvides a random number with power-of-ten distributions.
Properties
min_valThe minimum limit of val.
max_valThe maximum limit of val.
Functions
newCreate a random number object.

Types

dist_bin

typedef struct { int min_val; int max_val; byte unsigned wt /*= 0*/; } dist_bin

Defines the structure of a random distribution bin.

min_valThe minimum value of a bin.
max_valThe maximum value of a bin.
wtThe distribution weight of a bin.

random_util

class random_util

Provides a utility function to generate random numbers.

Summary
Functions
random_boolstatic Returns a randomized Boolean value based on the specified percentage.

Functions

random_bool

static function bit random_bool(int unsigned true_pct =  50)

static Returns a randomized Boolean value based on the specified percentage.

Argument

true_pctoptional The probability of the returned value to be randomized to 1 (true).  The unit is a percent.  The default is 50 (50% true).

Returns

The randomized Boolean value (1 or 0).

Example

bit rb = random_util::random_bool( .true_pct( 70 ) ); // 70% true

random_2_bin_num

class random_2_bin_num

Provides a random number using two distribution bins.

Example

The value of variable n.val is randomzed to between 0 and 9, or 10 and 19 with a weighted ratio of 1-2.

random_2_bin_num n = new();

//          min max wt
n.db[0] = '{  0,  9, 1 }; // bin 0
n.db[1] = '{ 10, 19, 2 }; // bin 1

assert( n.randomize() );
$display( n.val );
Summary
Properties
dbRandom distribution bins.
valrand The randomized value.
Functions
newCreate a random number object.

Properties

db

dist_bin db[2]

Random distribution bins.  The distribution weight of unused bins are set to be 0.

val

rand int val

rand The randomized value.

Functions

new

function new(int default_val =  0)

Create a random number object.

Argument

default_valoptional The value of val before randomization.  The default is 0.

random_4_bin_num

class random_4_bin_num

Provides a random number using four distribution bins.

Example

The value of variable n.val is randomzed to between 0 and 9, 10 and 19, 20 and 29, or 30 and 39 with a weighted ratio of 1-2-3-4.

random_4_bin_num n = new();

//          min max wt
n.db[0] = '{  0,  9, 1 }; // bin 0
n.db[1] = '{ 10, 19, 2 }; // bin 1
n.db[2] = '{ 20, 29, 3 }; // bin 2
n.db[3] = '{ 30, 39, 4 }; // bin 3

assert( n.randomize() );
$display( n.val );
Summary
Properties
dbRandom distribution bins.
valrand The randomized value.
Functions
newCreate a random number object.

Properties

db

dist_bin db[4]

Random distribution bins.  The distribution weight of unused bins are set to be 0.

val

rand int val

rand The randomized value.

Functions

new

function new(int default_val =  0)

Create a random number object.

Argument

default_valoptional The value of val before randomization.  The default is 0.

random_8_bin_num

class random_8_bin_num

Provides a random number using eight distribution bins.

Example

The value of variable n.val is randomzed to between 0 and 9, 10 and 19, 20 and 29, 30 and 39, 40 and 49, 50 and 59, 60 and 69, or 70 and 79 with a weighted ratio of 1-2-3-4-5-6-7-8.

random_8_bin_num n = new();

//          min max wt
n.db[0] = '{  0,  9, 1 }; // bin 0
n.db[1] = '{ 10, 19, 2 }; // bin 1
n.db[2] = '{ 20, 29, 3 }; // bin 2
n.db[3] = '{ 30, 39, 4 }; // bin 3
n.db[4] = '{ 40, 49, 5 }; // bin 4
n.db[5] = '{ 50, 59, 6 }; // bin 5
n.db[6] = '{ 60, 69, 7 }; // bin 6
n.db[7] = '{ 70, 79, 8 }; // bin 7

assert( n.randomize() );
$display( n.val );
Summary
Properties
dbRandom distribution bins.
valrand The randomized value.
Functions
newCreate a random number object.

Properties

db

dist_bin db[8]

Random distribution bins.  The distribution weight of unused bins are set to be 0.

val

rand int val

rand The randomized value.

Functions

new

function new(int default_val =  0)

Create a random number object.

Argument

default_valoptional The value of val before randomization.  The default is 0.

random_16_bin_num

class random_16_bin_num

Provides a random number using sixteen distribution bins.  See random_8_bin_num for how to use this class.

Summary
Properties
dbRandom distribution bins.
valrand The randomized value.
Functions
newCreate a random number object.

Properties

db

dist_bin db[16]

Random distribution bins.  The distribution weight of unused bins are set to be 0.

val

rand int val

rand The randomized value.

Functions

new

function new(int default_val =  0)

Create a random number object.

Argument

default_valoptional The value of val before randomization.  The default is 0.

random_32_bin_num

class random_32_bin_num

Provides a random number using thirty-two distribution bins.  See random_8_bin_num for how to use this class.

Summary
Properties
dbRandom distribution bins.
valrand The randomized value.
Functions
newCreate a random number object.

Properties

db

dist_bin db[32]

Random distribution bins.  The distribution weight of unused bins are set to be 0.

val

rand int val

rand The randomized value.

Functions

new

function new(int default_val =  0)

Create a random number object.

Argument

default_valoptional The value of val before randomization.  The default is 0.

random_power_of_2_num

class random_power_of_2_num extends random_32_bin_num

Provides a random number with power-of-two distributions.  This class uses the following pre-populated distribution bins.

//           min    max  wt       bin
db = '{ '{     0,     0, 1 },   //  0
        '{     1,     1, 1 },   //  1
        '{     2,     2, 1 },   //  2
        '{     3,     3, 1 },   //  3
        '{     4,     4, 1 },   //  4
        '{     5,     7, 1 },   //  5
        '{     8,     8, 1 },   //  6
        '{     9,    15, 1 },   //  7
        '{    16,    16, 1 },   //  8
        '{    17,    31, 1 },   //  9
        '{    32,    32, 1 },   // 10
        '{    33,    63, 1 },   // 11
        '{    64,    64, 1 },   // 12
        '{    65,   127, 1 },   // 13
        '{   128,   128, 1 },   // 14
        '{   129,   255, 1 },   // 15
        '{   256,   256, 1 },   // 16
        '{   257,   511, 1 },   // 17
        '{   512,   512, 1 },   // 18
        '{   513,  1023, 1 },   // 19
        '{  1024,  1024, 1 },   // 20
        '{  1025,  2047, 1 },   // 21
        '{  2048,  2048, 1 },   // 22
        '{  2049,  4095, 1 },   // 23
        '{  4096,  4096, 1 },   // 24
        '{  4097,  8191, 1 },   // 25
        '{  8192,  8192, 1 },   // 26
        '{  8193, 16383, 1 },   // 27
        '{ 16384, 16384, 1 },   // 28
        '{ 16385, 32767, 1 },   // 29
        '{ 32768, 32768, 1 },   // 30
        '{ 32769, 65535, 1 } }; // 31

Example

random_power_of_2_num n = new();
assert( n.randomize() );
$display( n.val );
Summary
Properties
min_valThe minimum limit of val.
max_valThe maximum limit of val.
Functions
newCreate a random number object.

Properties

min_val

int min_val

The minimum limit of val.  The val is randomized to be at least this value.

max_val

int max_val

The maximum limit of val.  The val is randomized to be at most this value.

Functions

new

function new(int default_val =  0,
int min_val =  0,
int max_val =  65535)

Create a random number object.

Argument

default_valoptional The value of val before randomization.  The default is 0.
min_valoptional The minimum limit of val.  The val is randomized to be at least this value.  The default is 0.
max_valoptional The maximum limit of val.  The val is randomized to be at most this value.  The default is 65,535.

random_power_of_10_num

class random_power_of_10_num extends random_16_bin_num

Provides a random number with power-of-ten distributions.  This class uses the following pre-populated distribution bins.

//                min         max  wt       bin
db = '{ '{          0,          0, 1 },   //  0
        '{          1,          1, 1 },   //  1
        '{          2,          9, 1 },   //  2
        '{         10,         10, 1 },   //  3
        '{         11,         99, 1 },   //  4
        '{        100,        100, 1 },   //  5
        '{        101,        999, 1 },   //  6
        '{      1_000,      1_000, 1 },   //  7
        '{      1_001,      9_999, 1 },   //  8
        '{     10_000,     10_000, 1 },   //  9
        '{     10_001,     99_999, 1 },   // 10
        '{    100_000,    100_000, 1 },   // 11
        '{    100_001,    999_999, 1 },   // 12
        '{  1_000_000,  1_000_000, 1 },   // 13
        '{  1_000_001,  9_999_999, 1 },   // 14
        '{ 10_000_000, 10_000_000, 1 } }; // 15

Example

random_power_of_10_num n = new();
assert( n.randomize() );
$display( n.val );
Summary
Properties
min_valThe minimum limit of val.
max_valThe maximum limit of val.
Functions
newCreate a random number object.

Properties

min_val

int min_val

The minimum limit of val.  The val is randomized to be at least this value.

max_val

int max_val

The maximum limit of val.  The val is randomized to be at most this value.

Functions

new

function new(int default_val =  0,
int min_val =  0,
int max_val =  10_000_000)

Create a random number object.

Argument

default_valoptional The value of val before randomization.  The default is 0.
min_valoptional The minimum limit of val.  The val is randomized to be at least this value.  The default is 0.
max_valoptional The maximum limit of val.  The val is randomized to be at most this value.  The default is 10,000,000.
typedef struct { int min_val; int max_val; byte unsigned wt /*= 0*/; } dist_bin
Defines the structure of a random distribution bin.
class random_util
Provides a utility function to generate random numbers.
static function bit random_bool(int unsigned true_pct =  50)
static Returns a randomized Boolean value based on the specified percentage.
class random_2_bin_num
Provides a random number using two distribution bins.
dist_bin db[2]
Random distribution bins.
rand int val
rand The randomized value.
function new(int default_val =  0)
Create a random number object.
class random_4_bin_num
Provides a random number using four distribution bins.
dist_bin db[4]
Random distribution bins.
rand int val
rand The randomized value.
function new(int default_val =  0)
Create a random number object.
class random_8_bin_num
Provides a random number using eight distribution bins.
dist_bin db[8]
Random distribution bins.
rand int val
rand The randomized value.
function new(int default_val =  0)
Create a random number object.
class random_16_bin_num
Provides a random number using sixteen distribution bins.
dist_bin db[16]
Random distribution bins.
rand int val
rand The randomized value.
function new(int default_val =  0)
Create a random number object.
class random_32_bin_num
Provides a random number using thirty-two distribution bins.
dist_bin db[32]
Random distribution bins.
rand int val
rand The randomized value.
function new(int default_val =  0)
Create a random number object.
class random_power_of_2_num extends random_32_bin_num
Provides a random number with power-of-two distributions.
int min_val
The minimum limit of val.
int max_val
The maximum limit of val.
function new(int default_val =  0,
int min_val =  0,
int max_val =  65535)
Create a random number object.
class random_power_of_10_num extends random_16_bin_num
Provides a random number with power-of-ten distributions.
int min_val
The minimum limit of val.
int max_val
The maximum limit of val.
function new(int default_val =  0,
int min_val =  0,
int max_val =  10_000_000)
Create a random number object.