Randomization with inline constraint

In reply to kathula venkatesh:

I’m not exactly sure if I understood your requirements but something as ABD_91 suggested should work

// Code your testbench here
// or browse Examples
class base;
  rand bit [1:0] x;
  constraint cnt {soft x inside {2,3};}
endclass

class basic;
  base base_h;
  function new();
        base_h = new();
    assert(base_h.randomize());
  endfunction
endclass

class error;
  base base_h;
  function new();
        base_h = new();
    assert(base_h.randomize() with {x == 0;});
  endfunction
endclass


module test();
  error e_h;
  basic  b_h;
  initial begin
    e_h = new();
    b_h = new();
    $display("e_h = %p", e_h.base_h);
    $display("b_h = %p", b_h.base_h);
  end
endmodule


Gives the following output

**$> run
e_h = '{x:'h0}
b_h = '{x:'h3}
**
Keep in mind this is just an example and randomizing inside the constructor is not normal, but it was intended to show that error and basic are generating the base value expected for x (var in your case)

Check your simulator version if it supports soft constraints

HTH,

-R