Reputation: 127
I have a variable logic [31:0] id
which is not declared as rand
or randc
. I need different ids each time into an array logic [31:0] id_array [16]
.
logic [31:0] id;
logic [31:0] id_array [16];
foreach(id_array[i]) begin
std::randomize(id);
id_array[i] = id;
end
In the above code, there is a possibility of getting duplicate ids in the array. How do I change the code to get unique ids in the array ?
Upvotes: 2
Views: 3343
Reputation: 1373
One more option in case you haven't had enough...
logic [31:0] id_array [16];
std::randomize(id_array) with {
foreach ( id_array[i] )
foreach ( id_array[j] )
if ( i!=j )
id_array[i] != id_array[j];
}
BTW, @Tudor wrote a very nice blog on SystemVerilog array constraints. It's definitely worth reading.
Upvotes: 0
Reputation: 42788
I would change this to
logic [31:0] id;
logic [31:0] id_array [$];
repeat (16) begin
std::randomize(id) with {!(id inside {id_array});};
id_array.push_back(id);;
end
or just
logic [31:0] id_array [15];
std::randomize(id_array) with {unique {id_array};};
Upvotes: 4
Reputation: 19122
Another option for randomization:
logic [31:0] id_array [$];
std::randomize(id_array) with { // random numbers in ascending order
foreach(id_array[idx]) {
(idx>0) -> id_array[idx] > id_array[idx-1];
}
};
id_array.shuffle(); // randomize order
Upvotes: 2