Constraint solving order

Here’s a simple constraint I have -



class base;

  rand bit [1:0] typ;
  rand bit [3:0] len;
 
  constraint c_typ_len { 
    typ == 1 -> len > 10;
  }
endclass


My intention here is if typ equals 1, then len must be greater than 10.

When randomize() is called upon the object of the above class, what is the order in which typ and len are randomized? Do they both get randomized simultaneously or does typ get randomized first and then len is randomized?

In reply to tpan:
Hi,

They will be randomized simultaneously but the data generated will not violate any of the constraints.

In reply to tpan:
if your intention it tyope == 1 then len must be > 10 .


constraint c_typ_len { 
    typ == 1 -> len > 10;
    solve typ before len ;
  }

Howver randomization will randomize the type value to between 0,1,2,3 , whenver randomzied typ value is 1 then contraint will randomize len > 10.

In reply to tpan:
https://verificationacademy.com/forums/systemverilog/constrain-random-variable-result-enumerated-type#reply-48832

In reply to dave_59:

Thanks, Dave. Is my understanding for my above constraint right.

  • The probability to have typ=1 and len>10 is 5/53. I get this by listing all the valid typ-len values when randomized. So we have -
    typ=0; len=0-15 (total of 16 valid pairs)
    typ=1; len=11-15 (total of 5 valid pairs)
    typ=2; len=0-15 (total of 16 valid pairs)
    typ=3; len=0-15 (total of 16 valid pairs)
  • If I included a ‘solve typ before len’ constraint in my class, the probability to have typ=1 and len>10 is 1/4. Is this right?

In reply to tpan:

The first part without solve/before is correct. Note that the probability of
len>10
is 20/53

The second part with solve/before is not correct. There are still 53 solutions. The probability to have typ=1 and len>10 is now 1/4, or 13.25/53.
The probability of
len>10
is now (13.25 + 3*13.25 * 5/16)/53, or 25.67/53

In reply to dave_59:

Trying to figure out how you arrived at the probability no. for the 2nd scenario.

You made a good point that we still have 53 solutions. solve-before simply changes the distribution but not the total available set of soultions, am I right?

In reply to tpan:

Right.

I’m just weighting the probabilities together. Maybe it would make more sense if I wrote it as

13.25*(5/16) + 13.25*(5/5) + 13.25*(5/16) + 13.25*(5/16) 
--------------------------------------------------------
                          53