0
votes

Let's say we have a class with a bunch of random variables (around 100 rand variables). Now, I want to randomize only one of the variables from that class and the rest of the variables should be the same. How do I do that?

class rand_var extends vmm_data;
    rand bit abc;
    rand bit [3:0] cde;
    rand bit en;
    ...
endclass

Now from one of the task, I want to randomize abc only, not any other variables of rand_var.

task custom_task();
    rand_var rand1;
    rand1 = new config

    if (config.randomize () with {

    }) 
    else $display("randomization failed");
2

2 Answers

1
votes

The easiest way of randomizing a single variable is using the std::randomize instead of the randomize method of the class.

task custom_task();
    rand_var config;
    config = new();

    if (std::randomize(config.abc) with {
    /* constraints on config.abc */
    }) 
    else $display("randomization failed");

You can also use what's called in-line randomization control when calling the class randomize method as in

if (!config.randomize(abc)) $display("randomization failed");

But you have to be careful when doing it this way because all of the active class constraints in the config object must still be satisfied, even on variables not being randomized.

1
votes

One way is to call randomize and pass only the variable you want to be random. Refer to IEEE Std 1800-2017, section 18.11 In-line random variable control. Here is a complete example:

class rand_var;
    rand bit abc;
    rand bit [3:0] cde;
    rand bit en;
endclass

module tb;

task custom_task();
    rand_var rand1;
    rand1 = new();
    if (!rand1.randomize(abc)) $display("randomization failed");
    $display("%b %d %b", rand1.abc, rand1.cde, rand1.en);
endtask

initial repeat (10) custom_task();

endmodule

Output:

1  0 0
1  0 0
1  0 0
0  0 0
1  0 0
1  0 0
0  0 0
1  0 0
1  0 0
0  0 0

Only abc is random for each task call.


See also How to randomize only 1 variable in a class?