Constraint for most recent four numbers are unique out of stream of random number

In reply to dave_59:

Hi Dave,
I have follow up question on this post. Do simulators remember the last time randomized number? or is unique cyclic in nature? even if unique is cyclic everytime we randomize, how is it really remembering the old values and producing different values in the next randomization? Basically I wanted to know how exactly below code is working, and generating unique numbers to push in queue.

I used your code snippet as below mentioned CHANGE.

class A;
 typedef bit [2:0] number_t;
 rand number_t number;
 rand number_t recent_numbers[$];            //CHANGE: added rand and queue here
 constraint uniq_recent { unique {number, recent_numbers}; }
 
 function void post_randomize;
   recent_numbers.push_front(number);
   if (recent_numbers.size() > 4)
      void'(recent_numbers.pop_back());
  endfunction
endclass

module top();
  A a = new();
  initial begin
    repeat(20) begin
    if(!a.randomize()) $display("Please look into randomization issue");
    $display("recent_numbers = %p",a.recent_numbers);
    end
  end

endmodule : top

Below are the output results,
recent_numbers = '{'h1}
recent_numbers = '{'h3, 'h1}
recent_numbers = '{'h4, 'h7, 'h0}
recent_numbers = '{'h1, 'h2, 'h5, 'h6}
recent_numbers = '{'h3, 'h5, 'h7, 'h4}
recent_numbers = '{'h5, 'h6, 'h7, 'h0}
recent_numbers = '{'h7, 'h4, 'h2, 'h5}
recent_numbers = '{'h1, 'h0, 'h6, 'h3}
recent_numbers = '{'h6, 'h3, 'h0, 'h4}
recent_numbers = '{'h5, 'h1, 'h6, 'h0}
recent_numbers = '{'h7, 'h3, 'h0, 'h5}
recent_numbers = '{'h0, 'h4, 'h3, 'h2}
recent_numbers = '{'h1, 'h5, 'h0, 'h4}
recent_numbers = '{'h1, 'h7, 'h4, 'h6}
recent_numbers = '{'h7, 'h1, 'h3, 'h0}
recent_numbers = '{'h3, 'h4, 'h6, 'h1}
recent_numbers = '{'h4, 'h6, 'h1, 'h7}
recent_numbers = '{'h0, 'h4, 'h2, 'h5}
recent_numbers = '{'h6, 'h4, 'h2, 'h5}
recent_numbers = '{'h0, 'h7, 'h6, 'h5}

Thank you,
Mega