Array randomization

Hi,

Can someone share methods to randomize array to get unique max value?

Let’s say.

int unsigned A[9];

The solution has to meet 2 criteria:

1.all the elements inside {[0:9]}
2.max value of A[9] is unique. 

A possible solution is A[9] = '{0,0,0,0,0,0,0,0,1}.

Thanks.

In reply to mlsxdx:

class C;
   rand int A[9];
   rand int max;
   constraint c_max { max inside {[1:9]};}
   constraint c_1 { foreach(A[i]) A[i] inside {[0:max]};}
   constraint c_2 {A.sum(item) with (int'(item == max)) == 1;}
endclass

In reply to dave_59:

class B;
   rand int A[9];
   int mq[$],mi[$];

   constraint c_1 { foreach(A[i]) A[i] inside {[0:9]}; }

   function void get_it(int depth=0);
      this.randomize();
      mq = A.max();
      mi = A.find(x) with (x == mq[0]);
      
      if(mi.size() == 1) begin
         print_it();
	 return;
      end else begin
	 get_it(depth+1);
      end
   endfunction // get_it

   function void print_it();
      $display("mq:%p, A:%p",mq,A);
   endfunction // print_it

endclass // B

In reply to dave_59:

Hi Dave ,

What does the int’() cast do in your sample code ?

Normally when we use it in sum() it casts size of item to be 32 bits but in this case we use ::
item == max .

Won’t it automatically append bits to the smaller one ? ( In case item && max are unequal size ) thereby making the int’() unnecessary ?

Thanks .

In reply to Have_A_Doubt:

Normally the result of the
sum()
method is the same as the array’s elements, 32-bits. But when using the
with
clause, the result is the size of expression inside.
(item == max)
is a Boolean equality expression whose result is 1-bit, true or false. So you need the cast back to
int
to get 32-bits.

In reply to dave_59:

Hi Dave ,

Need you thoughts on the following ::

Are (1) && (2) always Similar ? ::


(1) A.sum with ( int' ( item == max ) ) == 1 ;
(2) A.sum(item) with ( int'( item = max ) ) == 1 ; // Iterator_argument Specified

In LRM 7.12 Array Manipulation Methods it says ::

[ ( iterator_argument ) ] is Optional .

So if we were to specify any other iterator_argument name ( i.e other than item ) we would need to explicitly specify it .


Ex :: A.sum( x ) with ( int ' ( x == max ) ) == 1 ; 

Thanks

In reply to Have_A_Doubt:

Yes, they are the same. Typically you only use an iterator_argument if you wanted something more descriptive than item, or when nesting these methods.

In reply to dave_59:

In reply to mlsxdx:

class C;
rand int A[9];
rand int max;
constraint c_max { max inside {[1:9]};}
constraint c_1 { foreach(A[i]) A[i] inside {[0:max]};}
constraint c_2 {A.sum(item) with (int'(item == max)) == 1;}
endclass

Hello Dave,

Can A.max() system function be used in the constraint ? I see an error saying array equality feature is not yet supported. I am thinking of something like this.

class uniquemax; 
  rand int unsigned a[9];
  rand int q[$];
  constraint a_c { 
    foreach(a[i]) 
      a[i] inside {[0:9]}; 
    q.size()==1;
    q[1]==8;
    a.max()==q;
  //  a.sum() with (int'(item==a.max()))==1;
  }

Thanks

In reply to verif_gal:

There are a number of problems with the code you just posted.

If you constrain
q.size()==1
, then only element
q[0]
will exist.

You cannot compare an int (
a.max()
) with a queue, event if the queue has only one element. You need to write it as
a.max()==q[0]
. But I’m not sure why you would want use a queue constrained to one element.

You constraints would not meet the original criteria of having only one array element with the max value.

In reply to dave_59:

In reply to verif_gal:
There are a number of problems with the code you just posted.
If you constrain
q.size()==1
, then only element
q[0]
will exist.
You cannot compare an int (
a.max()
) with a queue, event if the queue has only one element. You need to write it as
a.max()==q[0]
. But I’m not sure why you would want use a queue constrained to one element.
You constraints would not meet the original criteria of having only one array element with the max value.

I created a queue with only one element, because I was under the impression that array locator methods like .max() returns a queue with maximum element in it.
What I am trying to understand here is : if a system function .max() could be used to meet the constraint criteria.

Thank you

Hi Dave,

Here max should not repeat right but the result is repeated.
‘{A:’{0, 6, 6, 7, 2, 9, 8, 1, 0} , max:9}
‘{A:’{6, 7, 2, 6, 0, 5, 6, 2, 5} , max:7}
‘{A:’{7, 1, 6, 8, 6, 9, 2, 6, 5} , max:9}
‘{A:’{6, 9, 7, 5, 3, 8, 1, 3, 5} , max:9}
‘{A:’{0, 9, 4, 2, 7, 7, 5, 2, 3} , max:9}

Thanks !!!

In reply to UVM_geek:

That was not part of the original example description. Only that the max value appear once in the set of 9 array elements.