Randomization of a variable before all other variables

I have my seq item class in which I want to randomise one variable before all other variables, e.g let’s say I want to solve a before all other variables in following example:

class seq_item extended from class umv_sequence_item;
   rand bit [7:0] a,b,c,d ,e ...... ,x,y,z;
   constraint prob{ solve a before b;
                    solve a before c;
                    solve a before d;
                    .....
                    .....
                    solve a before z;
                   }
    endclass

To solve a single variable before all other, I have to write solve constraint for all the rand variables present in the class. This is easy when we have few rand variables, but tedious when a lot of variables are present in the class. Is there any other better way to do what I am trying to do in above example. I will really appreciate your help.

1 Like

In reply to 8Blades:

If a variable ‘a’ is ‘randc’, it will be solved before ‘rand’ variables.

Also, second approach:



class my_class;
  rand bit [7:0] a;
endclass

class seq_item extended from class umv_sequence_item;
   bit [7:0] a; // not random 
   my_class my_class_h;
   rand bit [7:0] b,c,d ,e ...... ,x,y,z; // 'a' is removed
   /* constraint prob{ solve a before b;
                    solve a before c;
                    solve a before d;
                    .....
                    .....
                    solve a before z;
                   }
    */

   function void pre_randomize();
      my_class_h = new(); my_class_h.randomize();
      a = my_class_h.a;
   endfunction: pre_randomize
 
    endclass

In reply to 8Blades:

Hi 8Blades,

No need to write solve…before statement multiple times.
It can be taken care using single statement only like mentioned below -

constraint prob{ solve a before b,c,d,…,z;}

In reply to DigvijayS:

Using solave a before … applies only the constraints to a first.
If you want to randomize a variable with constraints before the other variables you should do this in a task which is called prior to the body task. Reasonable tasks are pre_body or pre_do.

In reply to chr_sue:

Using solve a before … applies only the constraints to a first.

If the constraint is applied first, doesn’t it essentially mean that the variable is resolved before any other variable?

If you want to randomize a variable with constraints before the other variables you should do this in a task which is called prior to the body task. Reasonable tasks are pre_body or pre_do.

How is it possible to randomize a single variable from the pre_body task of sequence and then randomize all other variables in the body task?

In reply to 8Blades:

solve a before b
has nothing to do with the order of applying constraints. It has to do with the order that values are picked from set of values that satisfy all the constraints simultaneously.

Conceptually, the constraint solver builds a solution space consisting of all the possible sets of values that satisfy the constraint. It then randomly picks one set of values uniformly from that space for the complete set of random variables. Sometimes, certain value combinations are rare (see the example in section 18.5.10 Variable ordering in the 1800-2012 LRM).

The
solve a before b
construct still goes through the same process of building a solution space, but instead of picking one set of values for all the random variables, it picks a value for each random variable uniformly from the set of legal values for that variable in the order specified. Each pick may reduce the set of values available for the next variable. So the only effect this has in the distribution of values seen as a result.

It would help to know what you are trying to achieve as there may alternatives. Some constraints, like those on the size of a dynamic array have implicit ordering. A
dist
constraint may also give you results you are looking for.