Can class withing another class could be randomized?

Hi,
I have a piece of code like this:


...
class wol_case;
  class rand_pkt;
      rand bit [7:0]  pkt[];
      constraint pkt_c1 {pkt.size() inside {[72:136]};
                         pkt[0] == 8'h12;
                         pkt[1] == 8'h34;
                        };
  endclass: rand_pkt

  rand_pkt rand_pkt_inst;
 
  function new();
      rand_pkt_inst = new;
  endfunction
endclass: wol_case

wol_case wol_case_inst;

initial begin
  wol_case_inst = new;
  #10;
  if (~wol_case_inst.rand_pkt_inst.randomize()) $display("DBG: randomize failed!")
end

  

It is very interesting when I run this piece of code, I always get information that the randomized failed. But If I remove class wol_case and only lef class rand_pkt and run the code again, then the randomize is ok. Does this mean class.randomize could not be run in embedded class? thanks.

In reply to cy_skyworld:

The problem with the code shown is you are using the bitwise not (~) instead of the logical not(!).

It always helps to show a complete runnable example. I can only guess in your second attempt that you used a different operator.

In reply to dave_59:

Hi Dave,
thanks for your kind reply. Your reply works. But I’m confused here: what is the difference between this “~” and “!”? For one bit signal, I guess they should both work the same effect.

Regards

In reply to cy_skyworld:

The return type of randomize() is int, not bit.

In reply to dave_59:

thanks