Constraint a variable so that it's less likely to retain its current value

Hi!
I want to write a constaint for a 1 and 2 bits of logic variables so that each time I randomize, the probability of that logic to be the same value as before will be %5 for example. So its less likely to retain its value, it wants to change. How would I do that? Thanks in advance.

In reply to emin:

You can use this general constraint

class A;
  rand bit [4:0] value;
  // only 5% of the time, bits [1:0] will be the same as the previous bits
  constraint keep { value[1:0] == const'(value[1:0]) dist {1:=5,0:=95}; }
endclass

module top;
  
  A a=new;
  initial repeat(100) begin
    assert(a.randomize());
    $displayb(a.value);
  end
endmodule

Thank you Dave. Is below variable distribution valid? Or do I have to change distributions via variables by overriding pre_randomize function?


constraint keep {
  if(!value) 
    value[1:0] == const'(value[1:0]) dist {1:=5,0:=95}; 
  else
    value[1:0] == const'(value[1:0]) dist {1:=95,0:=5};
}

In reply to emin:

It is extremely difficult to use multiple distribution constructs with random constraint interdependencies. You should use a signal distribution controlled by non-random variables. These can be set with pre_randomize or anytime before calling randomize.

In reply to dave_59:
Great! Thank you Dave.

In reply to dave_59:

Can you explain the syntax and meaning of const’(value)?

Does const’() check the value of the variable in the previous cycle?

constraint keep { value[1:0] == const’(value[1:0]) dist {1:=5,0:=95}; }

In reply to hkc:

Yes, it uses the current value as if it were a non-random state variable—same as a constant for randomization. This is defined in section 6.24.1 Cast operator.

In reply to dave_59:

In the example below addr picks the values 20 , 71,110 based on the distributions


class packet;
  rand bit [3:0] addr;
 
  constraint addr_range { addr dist { 20 := 5, 71 := 8, 110 := 12 }; }
endclass
 

I am trying to understand the example in this thread as we are comparing
LHS and RHS(old value) and use dist {1:=5,0:=95};

so what does that mean? it seems like a disconnection w.r.t the example i posted as normally we just have a variable and it picks from the values mentioned after dist based on the distribution

LHS == RHS is the expression and the expression being true (1) has a probability of 5% and the expression being false (0) has 95% probability
{ LHS == RHS) dist {1:=5,0:=95}; }

is my understanding correct? I haven’t seen such an example yet as most examples just talk about variables but seems like we could do the same for conditional expressions

In reply to hsam:

Correct. See Issue with following dist Constraint | Verification Academy