Partial array randomization

Hi,

For only randomizing some bits of the array and retaining value for other bits, example an 8 bit addr, only bits 6 and 3 need to be randomized. Rest remain the same.
In one of the forum questions, I saw this suggestion :

“You’ll need to create a separate variable. You can either randomize the separate variable and just move the bits you want into addr, or you can copy the value of addr to that variable before calling randomize and constrain the bits you don’t want randomized.”

How do I move in the randomized separate variables to the main array ? Should I use concatenation ?

Thanks

In reply to UVM_learner6:

Yes it is possible to randomizing some bits of variable like below.

rand byte a;

constraint a1{a[0]==1; };
constraint a2{a[3]==1; };
constraint a3{a[6]==1; }; // it will randomize all other bits except 0,3,6.

Outputs:
p.a=11011001
p.a=01111011
p.a=11011111

In reply to UVM_learner6:

See this. Although in your case, the mask is not random.

In reply to UVM_learner6:


class gen_data;

  rand logic [7:0] data;
  rand logic [7:0] mask;
  logic [7:0] old_data;
   
  //randomize only 3rd and 6th bit
  constraint c2 { mask == 8'b01000100; }
  //Randomize any 2 bits
  //constraint c2 { $countones(mask) == 2; }
  
  constraint c4 { (data & ~mask) == (old_data & ~mask);}
  function void post_randomize();
    old_data = data;
  endfunction 
 
  function void display();
    $display("data : %b , mask : %b ", data, mask);
  endfunction
  
endclass : gen_data


program my_prg;
  
  initial begin
    gen_data a;
    a = new();
    repeat(10) begin
      a.randomize();
      a.display();
    end       
  end
  
endprogram 


Output:


data : 01000000 , mask : 01000100 
data : 00000000 , mask : 01000100 
data : 01000100 , mask : 01000100 
data : 00000100 , mask : 01000100 
data : 01000000 , mask : 01000100 
data : 00000100 , mask : 01000100 
data : 00000100 , mask : 01000100 
data : 01000100 , mask : 01000100 
data : 00000100 , mask : 01000100 
data : 00000100 , mask : 01000100