How to Randomize Only 1 Variable in a Class?

How to Randomize Only 1 Variable in a Class?

I have the following class

class ABC;


    rand bit[2 : 0] mode; 
    randc bit[2 : 0] mode_cylic;
    constraint range{

        mode >= 2;
        mode < 6;

    };

    constraint range_cylic{

        mode_cylic >= 2;
        mode_cylic < 6;

    };



endclass 

I have a test object of the test class ABC, and I want to randomize only the mode variable in that object. How can I do that?

2 Answers

You have to be careful when randomizing a class and only randomizing certain variables because the constraints are deactivated separately from the variables. All active constraints must succeed for a class randomization.

The simplest thing to do when you only have one or a few variables out of a larger set of random variables to randomize is using the built-in std::randomize method. This randomizes the variables in its arguments regardless of whether they are class variables, and does not use any class constraints.

std::randomize(obj.mode);

Any constraints you want to use must be added using a with {expressions} constraint.

Another thing you can do is using the class randomize method, also putting just variables you want randomized.

obj.randomize(mode);

However all class constraint must still succeed, so mode_cylic must already have a value 2-5, or you need to turn off the constraint using

obj.range_cylic.constraint_mode(0);

You can do a similar thing using rand_mode() to turn off the random variables you don't want randomized.

I would use the rand/constraint_mode() for turning off one variable or constraint, rather than trying turn off everything except one. It becomes very difficult to maintain as you start adding more variables/constraints to your testbench.

Try to use std:randomize or even better, separate your classes in to objects to be randomized separately.

2

One way is to disable randomization for the variable you don't want to randomize:

class ABC;
    rand bit[2 : 0] mode; 
    randc bit[2 : 0] mode_cylic;

    constraint range{
        mode >= 2;
        mode < 6;
    }

    constraint range_cylic{
        mode_cylic >= 2;
        mode_cylic < 6;
    }
endclass

module tb;
    ABC abc = new();
    bit result;
    initial begin
        abc.range_cylic.constraint_mode(0);
        abc.mode_cylic.rand_mode(0);
        repeat (10) begin
            result = abc.randomize();
            $display("mode=%0d mode_cylic=%0d", abc.mode, abc.mode_cylic);
        end
    end
endmodule

Refer to IEEE Std 1800-2017, section 18.8 Disabling random variables with rand_mode().

This is the output I get:

mode=2 mode_cylic=0
mode=2 mode_cylic=0
mode=2 mode_cylic=0
mode=4 mode_cylic=0
mode=4 mode_cylic=0
mode=2 mode_cylic=0
mode=4 mode_cylic=0
mode=3 mode_cylic=0
mode=5 mode_cylic=0
mode=4 mode_cylic=0

Or, if you want mode_cylic to have a specific value within the range_cylic range, use with:

initial begin
    repeat (10) begin
        result = abc.randomize() with { mode_cylic == 5; };
        $display("mode=%0d mode_cylic=%0d", abc.mode, abc.mode_cylic);
    end
end

This is the output I get:

mode=2 mode_cylic=5
mode=2 mode_cylic=5
mode=2 mode_cylic=5
mode=4 mode_cylic=5
mode=4 mode_cylic=5
mode=2 mode_cylic=5
mode=4 mode_cylic=5
mode=3 mode_cylic=5
mode=5 mode_cylic=5
mode=4 mode_cylic=5

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

James H. Sterling
Author

James H. Sterling

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.