std::randomize - naveen0215/uvm_sequence_library GitHub Wiki
std::randomize() is also called a Scope-Randomize Function. Scope randomize gives you the ability to randomize variables that are not members of a Class and even multiple variables can be randomized at the same time with either of the methods shown below.
The scope randomize is generally done for the local variables which are declared without the rand function and also users can specify random constraints for it. Even it can accept the inline constraints using the โwithโ clause. Below there are some syntaxes for declaring the std::randomize function.
- Syntax:
std::randomize (variable);
std::randomize (variable) with { constraint's; };
std::randomize (variable-1, variable-2 ... variable-n);
std::randomize (variable-1, variable-2 ... variable-n) with { constraint's; };
- Example:
module std_random;
bit [3:0]a;
bit [3:0]b;
bit [3:0]c;
bit [3:0]d;
bit [3:0]e;
initial begin
repeat(4)begin
void'( std ::randomize(a));
$display("value of a %d",a);
void'(std::randomize(b) with {b>6; a<12;});
$display("value of b :%d",b);
void'(std::randomize(c,d) with {c<4; d<3;});
$display("value of c :%d and d %d ",c,d);
void'(std::randomize(e) with {e inside {[1:4]};});
$display("value of e :%d ",e);
end
end
endmodule
In the above example, we declared 5 local variables. These variables are randomized using std::randomize(), even all the variables can be randomized at the same time by passing multiple variables as arguments to the randomize() function. Even we can specify our own inline constraints to the randomizing variables.
This is an advantage for randomizing the local variables and even in the module we can randomize multiple variables at the same time just like class_handle.randomize();
- **Output: **

Using std::randomize inside the class:
If we randomize the variables using class_handle.randomize only those variables which are of type rand and randc are randomized. In order to overcome this we can use std::randomize(this), which will randomize all the variables of that class. But, the variables which are local to any function are not randomized, this is the main drawback of this std::randomize() function. For this local variables we need to randomize again using std function.
- Example:
class test;
int a;
rand bit [2:0]b;
constraint c1 { a >= 2;a <= 9; }
function void display();
int c;
if (std::randomize(this));
$display("using std_randomize %0d , %d",a,b);
if (randomize(a))
$display("randomize(a) %0d",a);
if (std::randomize(a));
$display("using std_randomize(a) %0d",a);
if(std::randomize(b))
$display("using std_randomize b: %0d",b);
if (std::randomize(c) with {c>1;
c<4;})
$display("using std_randomize with constraint,c: %0d",c);
if (this.randomize());
$display("this randomize %0d %d",a,b);
endfunction
endclass
module tb;
test t;
initial begin
t=new();
repeat (3)
t.display();
end
endmodule
In this example, we declared int type 'a' and rand bit type 'b' variables in the class test and inside the display function we declared local variable 'c'. So, when we use std::randomize(this) only the 'a' and 'b' variables are randomized leaving the local variable 'c. Even we can randomize the local variable and can give inline constraints by using "with" clause.
- Output:

From the above output we can see the exact operation of std:randomize(), this function is strictly applicable only to the local variables. Even though there is constraint defined for variable 'a' within the class, std::randomize(a) is not considering that global constraint as it is totally dependent on its inline constraint. Since there is no inline constraint for std::randomize(a), the simulator is considering the entire range of 'a'.
Whereas for variable 'c' we have declared a inline construct whose range is between 1 to 4. Hence, the output of c is between this range.
Limitations:
std::randomize(variable)considers only its inline constraint, or else it will consider its declared default range for randomization.std::randomize(this)is applicable only for randomizing the class variables. But this function will not consider the locally declared variables inside any function, hence there is more chance of missing some data during randomization.std::randomize(variable)is applicable only for locally declared variables. It cannot be accessed in any other functions or class.