Constraint to randomize elements of an array such that at least one is a multiple of 4

HI All,

I want to write a constraint to randomize an array with the following requirements:

a) Sum of all array elements must be 17
b) Array must have at least one element which is multiple of 4.
c) Size of an array could be random

class c;
  rand bit[7:0] arr[];

  constraint c1_arr_sz { arr.sum() with (int'(item)) == 17; 
                        arr.size() inside {[2:10]}; }
  constraint c2_arr_m4 { arr.or() with (item % 4 == 0); }

endclass

module m1;
  c obj;
  initial
   begin
     obj=new();
     repeat(5)
     begin
      obj.randomize();
      $display("array is %p",obj);
     end
    end
endmodule

Output of the code:

array is β€˜{arr:’{'h0, 'h3, 'h0, 'h0, 'h0, 'he, 'h0, 'h0, 'h0}}
array is β€˜{arr:’{'h8, 'h8, 'h0, 'h0, 'h0, 'h1}}
array is β€˜{arr:’{'h5, 'h7, 'h1, 'h4}}
array is β€˜{arr:’{'h4, 'h1, 'h7, 'h3, 'h2}}
array is β€˜{arr:’{'h1, 'h0, 'h3, 'h1, 'h0, 'h0, 'hb, 'h0, 'h0, 'h1}}

Multiple of 4, is being satisifed only in 2nd, 3rd and 4th iteration.
But it should be satisfied in 1st and last iteration also, may I know the reason?
How to update the code to get all requirements in all iterations.

Kindly share your opinion

Thank You

In reply to Thirumalesh Kumar:

0 % 4 == 0

Hi Dave, I modified the code to have one element (first elemnt)in array should have value =4.

class c;
  rand bit[7:0] arr[];

  constraint c1_arr_sz { arr.sum() with (int'(item)) == 17; 
                        arr.size() inside {[2:10]}; }
  constraint c2_arr_m4 { arr.or() with (0 % 4 == 0); }

endclass

module m1;
  c obj;
  initial
   begin
     obj=new();
     repeat(5)
     begin
      obj.randomize();
      $display("array is %p",obj);
     end
    end
endmodule

Output::

array is β€˜{arr:’{'hf, 'h2, 'h0} }
array is β€˜{arr:’{'hc, 'h4, 'h1, 'h0} }
array is β€˜{arr:’{'h2, 'h7, 'h6, 'h1, 'h0, 'h1, 'h0} }
array is β€˜{arr:’{'h0, 'h1, 'h7, 'h0, 'h3, 'h6, 'h0, 'h0} }
array is β€˜{arr:’{'h0, 'ha, 'h7} }
Please let me know if there is a issue in my code.

Please format your code making your code easier for others to read. I have done that for you.

I believe the original poster was expecting β€œa multiple of 4” to be a β€œnon-zero multiple of 4”. So the constraint for that would be

constraint c2_arr_m4 { arr.or() with (item > 0 && item % 4 == 0); }

What’s the use of arr.or here?

Give you β€œat least one” condition. You could also use a slightly more complex sum();

constraint c2_arr_m4 { arr.sum() with (int'(item > 0 && item % 4 == 0)) > 0; }
foreach(arr[i]) {
     if(arr[i] !=0)
          (arr[i] % 4 == 0) -> 1 inside {1}; 
}

@Shashank_c03, what is the purpose of your post–it would have no effect as a constraint. (it is unconstrained)

Hi Dave,

I have a doubt on how to write below constraint using sum and or methods.

  1. Array size between 4:10;
  2. Sum of array elements = 17.
  3. 2 elements in an array should be multiple of 4 and remaining elements can be any value.
 class simple;
    
       rand bit[4:0] arr[];
    
       constraint c_values{
           arr.size() inside {[4:10]};
           arr.sum() with(int'(item)) == 17;
       }
      constraint c_divide_4{
           arr.or() with(int'(item >0 && item%4 ==0)) ==2;
      }    
  
endclass
  
  initial begin
    simple s = new();
    s.randomize();
    $display("arr values = %0p", s.arr);
    
  end

Results::
Seeing constraint solver issue for this code.

Please format your code making your code easier for others to read. I have done that for you.

Usee arr.sum() instead of arr.or().

Thanks Dave.

Modified the constraint as below and it’s working.

constraint c_values{

  arr.size() inside {[4:10]};
  arr.sum() with(int'(item)) == 17;
  arr.sum() with(int'(item >0 && item%4 ==0)) ==2;
  
}

Note:: In what conditions should we use arr.or() function?

When you want logical OR reduction of elements.

Hi Dave,

Could you share the material to understand array methods completely?

I was trying to write a constraints using array method , but getting stuck?

1 Like