System Verilog: Random Number System Functions

System Verilog provides system functions – $urandom(),$urandom_range() and $srandom() for generating random numbers.

The $random verilog system function has only one random number generator shared between all threads, but each thread in simulation has its own random number generator for $urandom and $urandom_range. Separate random number generators for each thread helps to improve random stability

$urandom()
The $urandom() system function provides a mechanism for generating pseudo-random numbers. The function returns a new unsigned 32-bit random number each time it is called.

The seed is an optional argument that determines the sequence of random numbers generated.
This generates 32 bit unsigned number, but 2 bits, 4 bits etc. random numbers can be generated as shown below.

$urandom_range()
The $urandom_range() function returns an unsigned 32 integer within a specified range. Range would be specified using max and min values as shown below

In the below code addr1 range is from 32’hFFFF_FF00 to 32’hFFFF_FFFE. So it generated following values in 5 iterations.

srandom()
The srandom() method allows manually seeding the RNG of objects or threads.

The srandom() method initializes an object’s RNG using the value of the given seed.

In the below code 5 iterations are done without setting a seed using $srandom(). So different random numbers are generated. But once we set srandom(1), RNG will generate same random numbers. This will change once we change the seed.

2 Comments

  1. ABHILASH

    i executed the program using $srandom(1). but i didn’t get same random numbers using $srandom. will you check once the given code is proper. i am using questasim simulation tool

  2. PARTH GANDHI

    Hey Srini,

    what is the difference between $srandom and $urandom:
    1. as $urandom (i) produce same results as $srandom = 1
    2. Further, as far as I have read, for each individual seed, numbers should be different i.e. Seed 1 – 5 unique (not identical) values should be different from seed 2 – 5 unique values but by using $srandom = 1 or $urandom(i) it gives all 5 values of each seed set as exact same values.
    Can you please explain what is the scenario here?

    class srand_sysfn_class;
    rand bit [31:0] addr;

    function new();
    for(int i =1; i<5;i++) begin
    for (int j =0;j<5;j++) begin
    addr = $urandom(i);
    $display ("addr = %h ",addr);
    end
    $display("*****************");
    end

    `default_nettype none;

    for(int j = 1;j<5 ;j++) begin
    int seed = j;
    $display(" seed is set %0d",seed);
    void'($urandom(seed));
    for(int i = 0;i < 5; i++) begin
    addr = $urandom();
    $display ("addr = %h ",addr);
    end
    $display("—————");
    end

    endfunction
    endclass

    program p1;
    srand_sysfn_class c3 = new();
    endprogram

    Output:

    addr = 2fd9a2ac
    addr = 2fd9a2ac
    addr = 2fd9a2ac
    addr = 2fd9a2ac
    addr = 2fd9a2ac
    *****************
    addr = dfb76d78
    addr = dfb76d78
    addr = dfb76d78
    addr = dfb76d78
    addr = dfb76d78
    *****************
    addr = 9d5ab7e5
    addr = 9d5ab7e5
    addr = 9d5ab7e5
    addr = 9d5ab7e5
    addr = 9d5ab7e5
    *****************
    addr = 3feed2e4
    addr = 3feed2e4
    addr = 3feed2e4
    addr = 3feed2e4
    addr = 3feed2e4
    *****************
    seed is set 1
    addr = f377581d
    addr = 8ba1adbf
    addr = 131ab2c9
    addr = 3aae165d
    addr = 85e1726a
    —————
    seed is set 2
    addr = b4ec20a3
    addr = 17585bbe
    addr = b73555d3
    addr = 17dd0aa8
    addr = 42ce7d9f
    —————
    seed is set 3
    addr = f422e8af
    addr = 86446707
    addr = ea6fcc63
    addr = 76592177
    addr = eb04e4bc
    —————
    seed is set 4
    addr = 79d8d00f
    addr = 0ea2a3fe
    addr = 6f6ffbe6
    addr = 0f2f31c3
    addr = cdf86a3f
    —————

Leave a Reply

Your email address will not be published. Required fields are marked *