For loop and a random variable

HI ,

I have a scenario where i have to randomize the loop count and randomize the variable such the loop count should not be wasted due to duplication of randomization of variable.

for Eg::

sa_entry_row        = $urandom_range(1,5);//Randomizing  The number of times add entries
                      for(int j=0; j<sa_entry_row; j++) begin
                      entry_add_index = $urandom_range(0,($size(test_entries_add)-1));//Randomizing the SA_entry index
                      //this condition will check existing added list in queue and avoid duplication
                      if (!(entry_add_index inside test_entries_added)) begin

if we see the code, sa_entry_row count gets unused if the entry_add_index is already existing, how should i make sure that entry_add_index is unique for each sa_entry_row?

Thanks

In reply to syed taahir ahmed:

It would really help to show the declarations of all variables involved and what they represent.

From what I can figure out, you should be using constraints.

randomize (entry_add_index) with {
    entry_add_index <$size(test_entries_add);
    unique {entry_add_index,test_entries_added};
}

In reply to dave_59:

This is my full code. The issue is for eg:: when i randomize sa_entry_row ,if it is 5 and when entry_add_index is randomized and if the value already in test_entries_indices_added for all 5 times then the loop of 5 goes unused .

I have to make sure that , the variable entry_add_index is unique always when sa_entry_row is looped.

if (sa_op_type == ADD) begin
                    sa_entry_row        = $urandom_range(1,5);//Randomizing  The number of times add entries
                    for(int j=0; j<sa_entry_row; j++) begin
                        entry_add_index = $urandom_range(0,($size(test_entries_add)-1));//Randomizing the SA_entry index
                        //this condition will check existing added list in queue and avoid duplication
                        if (!(entry_add_index inside test_entries_indices_added)) begin
                            add_entries_random(test_entries,test_entries_add,entry_add_index);
                            test_entries_indices_added.push_back(entry_add_index);//Queue created to avoid the duplication of the entry
                            added_entry_indices.push_back(entry_add_index); //This is added entries which used as grep in delete entry
                        end
                    end
                end

Thanks

In reply to syed taahir ahmed:

You still didn’t show the declarations of all variables in the code. But I think replacing

entry_add_index = $urandom_range(0,($size(test_entries_add)-1));

with

void'(randomize (entry_add_index) with {
    entry_add_index <$size(test_entries_add);
    unique {entry_add_index,test_entries_added};
});

should work.

In reply to dave_59:

I’ll post the result once trying.

I appreciate your reply Dave, thanks

In reply to dave_59:

In reply to syed taahir ahmed:
You still didn’t show the declarations of all variables in the code. But I think replacing

entry_add_index = $urandom_range(0,($size(test_entries_add)-1));

with

void'(randomize (entry_add_index) with {
entry_add_index <$size(test_entries_add);
unique {entry_add_index,test_entries_added};
});

should work.

HI Dave ,

One doubt i have with the implementation, since there is condition the number of entry_add_index to be added will be count of $size(test_entries_add).

The query was if the sa_entry_row get a value 5 and we have only 3 entries to be added then the randomization will fail for unique? if i understand correctly.

Thanks

In reply to syed taahir ahmed:

For the third time, I’ll ask for the declarations of the variables involved. How does one determine how many remaining entries are left?

In reply to dave_59:

In reply to syed taahir ahmed:
For the third time, I’ll ask for the declarations of the variables involved. How does one determine how many remaining entries are left?

Sorry my bad … But your solution worked :smiling_face:

Thanks Dave.