Can anyone plz help me with this constraint question…
Take a rand variable with array size 10,need to get unique values in each location without using unique keyword and for any of 2 locations we need to get same value?
In reply to burra thanuja:
Hi burra.,
Please refer below solution for ur doubt,
// Code your testbench here
// or browse Examples
module top;
class test;
rand bit[3:0]array[10];
constraint cnst_array{
foreach(array[i])
{
foreach(array[j])
{
if(i!=j)
{
array[i] != array[j];
}
}
}
}
function void post_randomize();
//array[$urandom_range(0,9)]=array[$urandom_range(0,9)]; // below is alternate for
//this solution
int i=$urandom_range(0,8);
array[i]=array[i+1]; //though itseems everytime alternate index will be having same
//values,that's fine as the main context is to have same value in
//any two different indexes.
endfunction
endclass
test t1;
initial begin
t1=new();
t1.randomize();
$display("array=%p",t1.array);
end
endmodule
HI,
Yes you are right.,
but we can do like below.,
function void post_randomize();
int i=$urandom_range(0,8);
array[i]=array[i+1];
endfunction
though itseems everytime alternate index will be having same values,that’s fine as the main context is to have same value in any two different indexes.
In reply to Yash_wanth12345:
in the post randomize both the indexes of lhs and rhs might randomize to be the same value and it won’t meet the criteria ‘2 values should be same’
eg: a[2]=a[2] can happen
Can anyone plz help me with this constraint question…
Take a rand variable with array size 10,need to get unique values in each location without using unique keyword and for any of 2 locations we need to get same value?
class unq_arr;
rand int unsigned d;
rand int unsigned arr[10];
constraint arr_c{
foreach(arr[i]) { arr[i] inside {[1:10]};}
foreach(arr[i]) { arr.sum() with (int'(item==arr[i])) == (arr[i]==d ? 2:1);}
}
In reply to Alokpati:
There is No constraint ( direct / indirect ) however to ensure this .
The constraint solver could solve the equation ( arr[i]==d ) to be 0 always thereby giving only Unique elements .
Agree… got your point. And strange my code didn’t fail when ran for loop of 100(check added in post_randomize to validate number of unique element).