need to randomize a variable in a class. I want to randomize only one variable not all.
This is what I am trying to do
class class1;
bit var1;
bit var2;
endclass
class1 object1 = new();
object1.randomize(object1.var1)
I am getting the below error
In ‘obj.randomize()’ function call with inline random variable control,
arguments are limited to the names of the properties of the calling object.
Expressions are not allowed as arguments in this context
Can anyone please help me on this
Thanks
Deepthi,
The arguments for the randomize method can be the property names and no need of using the object to refer the properties of the class as shown below.
module test();
class class1;
bit var1;
bit var2;
endclass
class1 object1 = new();
initial
begin
object1.randomize(var1);
$display("%p", object1);
end
endmodule
In reply to puttasatish:
Thank you puttasatish. Got it.
what is the difference between randomizing an objevt using the method I am using above and setting the rand_mode of the some variables(which need not be randomized) to “0” and randomizing the object. say like
var2.rand_mode(0);
object.randomize();
Thanks
Deepthi
In reply to Deepthip:
Deepthi,
If a variable is declared as rand type and you don’t want to generate a random value for that variable, then, you can disable it by using rand_mode(0).
rand_mode method can be used only on the variables which are declared with rand or randc qualifier. rand_mode method cannot be used on a variable which is not declared with rand or randc qualifier. In your snippet, since, both var1 & var2 are not declared as rand type, you cannot use rand_mode on these variables to generate random values for var1 & var2.
But, if you consider the randomize with arguments, it can be used to generate random values for the variables even though they are not declared as rand type.