Help with Constraint

How about the best of both worlds?


// Code your testbench here
// or browse Examples
module tb();

  class abc;
    rand bit [2:0] pattern;
    bit [31:0] data;
    constraint single_bit { $countones(pattern) == 1; }
    
    function void post_randomize();
      data = {(33/3){pattern}}; // I'm having trouble explicitly casting to 32 bits, but it still works...
    endfunction
  endclass

  abc d;

  initial begin
    d=new();
    repeat (10) begin
      assert(d.randomize());
      $display("value of data =%b",d.data);
    end
    $finish;
end
endmodule

Working example on EDAPlayground