Randomization and control

class data_base;
  rand bit (signed or unsigned) [3:0] a,b,c;
  
  constraint var_constraint{
    c == a+b;
  }
  
  task get_data();
    $display("a : %3d | b : %3d | c : %3d",a,b,c);
  endtask: get_data
endclass: data_base

program tb_top();
  data_base db;
  
  initial begin
    db = new();
    repeat(5)begin
      db.randomize(a);
      db.get_data();
    end
    
  end
endprogram: tb_top

Output: (Signed)**

a : -3 | b : 0 | c : 0
a : -3 | b : 0 | c : 0
a : -3 | b : 0 | c : 0
a : -3 | b : 0 | c : 0
a : -3 | b : 0 | c : 0 Note : a = -3 varies for simulation run

Output (unsigned) :

a : 15 | b : 0 | c : 0
a : 15 | b : 0 | c : 0
a : 15 | b : 0 | c : 0
a : 15 | b : 0 | c : 0
a : 15 | b : 0 | c : 0 Note : a = 15 varies for simulation run

But when teh variables are int:
Output :

a : 0 | b : 0 | c : 0
a : 0 | b : 0 | c : 0
a : 0 | b : 0 | c : 0
a : 0 | b : 0 | c : 0
a : 0 | b : 0 | c : 0 Note: c,b is 0 (will never randomize) thus a is always 0

constraint cases I don’t understand:

a>b
a<b
and above cases.

******* W H Y ? ********

When you specify a variable in the call to randomize(), all of the other variables become state variables and aren’t randomized, which means that their values never change. In this case, they are zero since they aren’t set to any value.

Why do you specify the variable ‘a’ in the call to randomize()?

Hi @cgales thanks for the response!
I only wanted to randomize variable ‘a’.
when I use the variables in ranged bit widths , the behavior is completely unpredictable and hard to understand.

I tested your code on 3 different EDA tools with random seeds.
In all 3 cases, a is randomized to 0 ( state variables b & c retain their default value of 0 due to bit/int type )

The following results should never be observed in presence of ‘var_constraint’

a : 15 | b : 0 | c : 0
[ OR ]
a : -3 | b : 0 | c : 0

Irrespective of the data type of the 3 variables your constraint is equivalent to

 0 == a + 0 
 // which is basically
 0 == a
1 Like

I believe the point you might be overlooking is that when calling randomize() on a class object, all active class constraints must be satified, regardless of whether the variables used in the constraint expressions are random or not.

If you just want to randomize a variable by itself, call std:: randomize(a) with { your_constraints};