How to set the values in serial on getting random order from dynamic array

Hi ALL,

My Requirement is to make serial input in dynamic array. After getting random values from the array.Below is the example to make more understandable:

getting below value from the dynamic array A whose size is 12.
value[0] = 3;
value[1] = 2;
value[2] = 2;
value[3] = 1;
value[4] = 1;
value[5] = 4;
value[6] = 3;
value[7] = 2;
value[8] = 4;
value[9] = 4;
value[10] = 1;
value[11] = 3;
desired array should be as below:
temp[0] = 3;
temp[1] = 2;
temp[2] = 1;
temp[3] = 4;
temp[4] = 3;
temp[5] = 2;
temp[6] = 1;
temp[7] = 4;
temp[8] = 3;
temp[9] = 2;
temp[10] = 1;
temp[11] = 4;

as it is dynamic array , flexibility to change the size on running again the test,so sometime size can be less example 8, then below is getting value;
value[0] = 12;
value[1] = 27;
value[2] = 29;
value[3] = 16;
value[4] = 16;
value[5] = 27;
value[6] = 12;
value[7] = 29;
then desired will be
temp[0] = 12;
temp[1] = 27;
temp[2] = 29;
temp[3] = 16;
temp[4] = 12;
temp[5] = 27;
temp[6] = 29;
temp[7] = 16;

In reply to myselfprakhar:

using array method shuffle()


// assume value[] is given
temp = new[value.size](value);
temp.shuffle();

In reply to javatea:
Hi,

Shuffle is not helping I tried it. But it will again give some random sequence.
AS output of below :
value[0] = 3;
value[1] = 2;
value[2] = 2;
value[3] = 1;
value[4] = 1;
value[5] = 4;
value[6] = 3;
value[7] = 2;
value[8] = 4;
value[9] = 4;
value[10] = 1;
value[11] = 3;
will be
temp[0] = 1;
temp[1] = 4;
temp[2] = 4;
temp[3] = 3; and so on… while requirement is all unique come in starting as in this case 4 values are unique so first four index will be with unique, same for next four index.

In reply to myselfprakhar:

can you describe in English?
i really don’t understand what you are talking about.

In reply to javatea:
If you see the example, the stored values in the value array is in random order. and my requirement is to rearrange the values so that first all unique value get stored in temp array.(if you see all value are in equal number i.e. number of 1,4,3,2 all are equal i.e. 3).
Shuffle just randomize again the complete array, It don’t made first all set of unique value then again all set of unique value.
Thanks,

In reply to myselfprakhar:

Anyone can see that unique word in your first post example or topic ???
let me know, I will hire this guy.

back to the question: i.g. sub_group has 4 unique items
value: proper size (4,8,12…) with proper item occurrence (4*N)
temp: the order you can’t generate, unique each subgroup

Don’t you think to have valid temp first, then use shuffle() to have value?
How you guarantee your value has valid content? from constraint?
From my experience, temp value should be generated at the same place.

Anyway if you give me value with proper content, then I have no idea about how unique subgroup size is.
Here you go:


// given valid value[] and calculating temp[]
int tbl[int];
int q[$];
int temp[] = new[value.size];

foreach (value[i]) begin
  if (!tbl.exists(value[i]))
    tbl[value[i]] = 1;
  else
    tbl[value[i]]++;
end

q = tbl.find_index() with(1); 

for (int i = 0; i < value.size; i+=q.size) begin
  if (i % q.size == 0) begin
     q.shuffle();
     for (int j = 0; j < q.size; j++)
        temp[i + j] = q[j];
  end
end

In reply to javatea:

THANKS…

int q[$];
q =A.unique();
foreach(A[i]) begin
 A[i] = q[i%q.size()];
end