How to generate the unique values in array with out using unique keyword

Hi All,

I am trying to generate the array with unique values. My requirement is if i run the code for multiple times it needs to generate the unique values multiple times.
I have code for generating the unique values but when i am running the code for multiple times it is generating the same values for all times.
what modification i need to have in the below code if i want different values for each run.

Thanks in advance.

//display the array elements with unique values 
class unique_array_example;
rand bit [7:0] array_1[];
//rand int i;
constraint addr_size{array_1.size()==5;}
constraint addr_unique_values{foreach(array_1[i])
array_1[i]==i*2;}
endclass:unique_array_example

module abc();
unique_array_example uaeg_1;
initial begin
uaeg_1 =new();
repeat(2)
begin
uaeg_1.randomize();
foreach(uaeg_1.array_1[i])
begin
$display("the value of array_size is %d ",uaeg_1.array_1.size());
$display("the value of a is %d ",uaeg_1.array_1[i]);
end
end
end
endmodule

Log file simulations/results
Compiler version L-2016.06-SP1_Full64; Runtime version L-2016.06-SP1_Full64; Aug 26 15:53 2021
the value of array_size is 5
the value of a is 0
the value of array_size is 5
the value of a is 2
the value of array_size is 5
the value of a is 4
the value of array_size is 5
the value of a is 6
the value of array_size is 5
the value of a is 8
the value of array_size is 5
the value of a is 0
the value of array_size is 5
the value of a is 2
the value of array_size is 5
the value of a is 4
the value of array_size is 5
the value of a is 6
the value of array_size is 5
the value of a is 8

In reply to Manikanta Kopparapu:
You need to explain your requirements in more detail. Right now your constraints generate a non-random series of numbers starting at 0 each time you randomize. We could change the constraint so that it continues the series where it left off, but that does not look very interesting. It’s totally non-random, and you would only be able to call randomize 16 times before the serious would have to repeat.

Also, when you say you want unique values between calls to randomize, are the values {0,2,4,6,8} considered unique from {2,4,6,8,10}?

It would really help to show examples of expected results you are looking for.

In reply to dave_59:

In reply to Manikanta Kopparapu:
You need to explain your requirements in more detail. Right now your constraints generate a non-random series of numbers starting at 0 each time you randomize. We could change the constraint so that it continues the series where it left off, but that does not look very interesting. It’s totally non-random, and you would only be able to call randomize 16 times before the serious would have to repeat.
Also, when you say you want unique values between calls to randomize, are the values {0,2,4,6,8} considered unique from {2,4,6,8,10}?
It would really help to show examples of expected results you are looking for.

Hi Dave,
Thanks for your explaination. My requirement here is when i repeat the process for two times i need different unique values for two simulations.

say for the first repeat(run) i got 0,2,4,6,8 and for the second repeat(run) i require values other than the first run values say 2,4,6,8,10 is also considered as unique only for me.
For doing that also how can i write a constraint to start over from where it left in the lst run.
So in the code where i need to modify for getting the above different unique values.
Thanks,
Manikanta K.

In reply to Manikanta Kopparapu:
You can add a state variable that increments each time you call randomize

class unique_array_example;
  rand bit [7:0] array_1[];
  int offset;
  constraint addr_size{array_1.size()==5;}
  constraint addr_unique_values{
      foreach(array_1[i]) array_1[i]==(offset+i*2);}
  function void post_randomize();
    offset++;
  endfunction
endclass:unique_array_example

Again, this series of numbers is not random; it make little sense to do it this way except if this were a homework/interview question.

In reply to dave_59:

In reply to Manikanta Kopparapu:
You can add a state variable that increments each time you call randomize

class unique_array_example;
rand bit [7:0] array_1[];
int offset;
constraint addr_size{array_1.size()==5;}
constraint addr_unique_values{
foreach(array_1[i]) array_1[i]==(offset+i*2);}
function void post_randomize();
offset++;
endfunction
endclass:unique_array_example

Again, this series of numbers is not random; it make little sense to do it this way except if this were a homework/interview question.

Hi Dave,
This is a problem as a part of my learning. Now with the code you have provided i am able to see the unique values for each run . Thanks for the information.