Dynamic array randomization with even and odd values

How to write a constraint for dynamic array having 10 elements with

  • Odd values for 4 even indexes out of 5 even indexes
  • Even values for remaining indexes (1 even index + 5 odd indexes)

In reply to JBV20:

I am not sure I understand your question. If your array has 10 elements, doesn’t that mean it has exactly 5 odd indices and 5 even indices ?

In reply to KillSteal:

My bad. There was a typo in my question. I have updated the same.

In reply to JBV20:


module top ;
  
  class odd_even ;
    
    rand int a[10];
    
    constraint c_aa {
      foreach(a[i]) {
        a[i] inside {[1:20]};
       if (i%2 == 0 && i < 8)  
          i%2 != a[i]%2;
        else if(i%2 )
          i%2 != a[i]%2;
        
        else 
          a[i]%2 == 0 ;
    }
               
    }
    
  endclass 
  
  initial begin 
    odd_even o = new ;
    o.randomize();
    $display("%p",o.a);
    
  end 
  
endmodule 

In reply to kddholak:

Thanks for the reply.

Your code is restrict to first 4 even indexes (0,2,4,6) which is not as per expectation. The constraint should be able to generate on any random 4 even indexes out of 5 even indexes.

In reply to JBV20:

constraint c1 {
  // only even indexes can have odd element values
  foreach (array[i])
    array[i] % 2 == 1 -> i % 2 == 0;
  // there can only be 4 odd elements
    array.sum() with (int'(item % 2 == 1)) == 4;
}