How can I randomize without repetition using std::randomize

I am trying to randomize an address and make sure that address has not been selected before. I have a few other constraints as well. Here is an example of what I am doing:

random_reg_addr = new[32];
random_reg_data = new[32];

  foreach(random_reg_addr[pkt_idx]) begin
    // randomize register address
    if (!std::randomize(random_reg_addr[pkt_idx]) with { 
        random_reg_addr[pkt_idx] inside {[24'h0:24'hFFFFFF]};
        (random_reg_addr[pkt_idx] % 4) == 0; // 4 byte alligned
      }) begin
        `uvm_fatal(get_name(), $sformatf("Failed to randomize reg address"))
    end
  end

How can I make it so that each address is unique. I keep trying and my implementation of it causes the randomization to fail.

Randomize the entire array as an aggregate and put the foreach loop inside the constraint. Here is a complete example

module top;
  bit [23:0] random_reg_addr[] = new[32];  
  initial begin
    if (!std::randomize(random_reg_addr) with { 
      foreach(random_reg_addr[pkt_idx]) {
        random_reg_addr[pkt_idx] inside {[24'h0:24'hFF]};
        random_reg_addr[pkt_idx] % 4 == 0; // 4 byte alligned
      }
      unique {random_reg_addr};
    })
      $fatal("Failed to randomize reg address");
    else
      foreach(random_reg_addr[pkt_idx])
        $displayh(random_reg_addr[pkt_idx]);
  end
endmodule
1 Like

This worked great, thanks. But now I am wondering how I can make sure each address is spaced far enough away from each other in the case that I am writing a burst of size x, I want to prevent them for overlapping and overwriting previous data during the test. I have a queue of all the random sizes for each packet. I am just wondering how would I check this within the foreach loop inside std::randomize