Randomization Question

Hi

I have an lut like this
idx elements
0 0,1,2
1 3,4
2 5
3 6,7,8,9
4 10,11
5 12,13
6 14,15

I want to now randomly select n no: of elements in ascending order where the idx of each element is unique and difference between two elements is less than 5.

Please help me write the constraint.

In reply to jyoveda:

Lots of missing information

  1. Is n a random number between 1-6 (or whatever number of indexes are in the LUT)
  2. Did you mean to say you can only pick one number from each index position?
  3. Did you mean to say the difference between adjacent elements is less than 5?

In reply to dave_59:

1.Yes,
2.Yes,
3.Yes

I put something like this, but I can’t get the unique index part down.

rand int n;
rand int elements[];

constraint_c {
        n inside {[1:6]};
        elements.size == n;
        foreach(elements[i]){
                                if(i>0) {
                                         elements[i] > elements[i-1];
                                         elements[i] - elements[i-1] <5;
                                         }
                             }
}

In reply to jyoveda:

class A;
  int lut[7][] = '{
    0: {0,1,2},
    1: {3,4},
    2: {5},
    3: {6,7,8,9},
    4: {10,11},
    5: {12,13},
    6: {14,15}
  };
  
  rand int n;
  rand int elements[];
  constraint c {
    n inside {[1:$size(lut)]};
    elements.size == n;
    foreach(elements[i]) {
      i>0 -> {
        elements[i] > elements[i-1];    // ascending order 
        elements[i] - elements[i-1] <5; // difference between adjacent elements is less than 5
      }
      elements[i] inside {lut};
    }
      foreach(lut[i]) // at most one element from each lut row
        lut[i].sum() with (int'(item inside {elements})) <=1;
  }
endclass
module top;
  A a = new;
  initial repeat(10) begin
    assert(a.randomize());
    $display("%p",a);
  end
endmodule
1 Like

In reply to dave_59:

This works perfect. Thank you!