Randomize a variable and make sure previous value is not equal to the current value

I have an object, it has 3 variables, all are rand. One of the variable i should be able to randomize such that the previous randomised value is different than the present randomised value.

What would be the best way to achive this ? ( I am not allowed to use randc)

In reply to ChyavanPhadke:

Hi, you can use the post_randomize method. Every time you write a constraint, you will add one more, which will ensure that rand variables are not equal to variables that are not rand and are holding previous randomized values.

In reply to ChyavanPhadke:

If you are only concerned about the new randomized value not being equal to only the previous value, then you can use the constraint “a != const’(a)”. If you want to exclude multiple previous values, you will want to create a dynamic array and add the generated value to the array in the post_randomize() method.


class transaction;
  rand bit a;
  rand bit b;
  rand bit c;
  
  constraint a_non_repeating { a != const'(a); }; // New value of 'a' != previous value
  
  function new();
  endfunction
  
  function void print();
    $display("Values are a: %0d  b: %0d  c: %0d", a,b,c);
  endfunction
endclass

module testbench();
  transaction txn;
  
  initial begin
    txn = new();
    repeat (20) begin
      if (!txn.randomize()) $display("Randomization failure!");
      else txn.print();
    end
  end
endmodule