Constraint Randomization Interview Question

In reply to Have_A_Doubt:
So far we saw 2 solutions:

  1. With the foreach construct
  2. With a rotate

The following table lookup solution was provided to me by a colleague;
it is more efficient than solving many constraints.


class c; 
  rand int position;
  bit[31:0] data; 
  constraint ct12 {position inside {[0:22]};}
  const bit [0:22][31:0] lookup  = {32'b00000000001111111111111111111111, //0
				32'b10000000000111111111111111111111,
				32'b11000000000011111111111111111111,
				32'b11100000000001111111111111111111,
				32'b11110000000000111111111111111111,
				32'b11111000000000011111111111111111, //  5 
				32'b11111100000000001111111111111111,
				32'b11111110000000000111111111111111,
				32'b11111111000000000011111111111111,
				32'b11111111100000000001111111111111,
				32'b11111111110000000000111111111111, // 10
				32'b11111111111000000000011111111111,
				32'b11111111111100000000001111111111,
				32'b11111111111110000000000111111111,
				32'b11111111111111000000000011111111,
				32'b11111111111111100000000001111111,  // 15
				32'b11111111111111110000000000111111,
				32'b11111111111111111000000000011111,
				32'b11111111111111111100000000001111,
				32'b11111111111111111110000000000111,
				32'b11111111111111111111000000000011,  // 20
				32'b11111111111111111111100000000001,
				32'b11111111111111111111110000000000};	
   
   function void post_randomize();
        data= lookup[position];
        //$display("position=%d, data=%b", position, data);
   endfunction : post_randomize
endclass 

module m; 
  bit clk; 
  initial forever #5 clk=!clk;  
  c c1=new(); 

  initial begin 
     repeat(50) begin
		@(posedge clk) #2; 
         	if (!randomize(c1)) $error();
	        $display("%t position=%d, c1.data=%b", $realtime, c1.position, c1.data);

	        ai_12: assert($countones(c1.data))
            else  $display("ERROR val = %b, countones= %d", c1.data,         
                       $countones(c1.data));
     end
     $finish; 
  end
endmodule  
/*
                    7 position=          6, c1.data=11111100000000001111111111111111
#                   17 position=          6, c1.data=11111100000000001111111111111111
#                   27 position=         15, c1.data=11111111111111100000000001111111
#                   37 position=         19, c1.data=11111111111111111110000000000111
#                   47 position=          6, c1.data=11111100000000001111111111111111
#                   57 position=         16, c1.data=11111111111111110000000000111111
#                   67 position=          8, c1.data=11111111000000000011111111111111
#                   77 position=          3, c1.data=11100000000001111111111111111111
#                   87 position=          6, c1.data=11111100000000001111111111111111
#                   97 position=         21, c1.data=11111111111111111111100000000001
#                  107 position=          3, c1.data=11100000000001111111111111111111
#                  117 position=          9, c1.data=11111111100000000001111111111111
#                  127 position=         14, c1.data=11111111111111000000000011111111
#                  137 position=         14, c1.data=11111111111111000000000011111111
#                  147 position=          6, c1.data=11111100000000001111111111111111
#                  157 position=          7, c1.data=11111110000000000111111111111111
#                  167 position=          5, c1.data=11111000000000011111111111111111
#                  177 position=          1, c1.data=10000000000111111111111111111111
#                  187 position=         15, c1.data=11111111111111100000000001111111
#                  197 position=          6, c1.data=11111100000000001111111111111111
#                  207 position=         19, c1.data=11111111111111111110000000000111
.....
#                  237 position=          7, c1.data=11111110000000000111111111111111
#                  247 position=          0, c1.data=00000000001111111111111111111111
#                  257 position=          1, c1.data=10000000000111111111111111111111
#                  267 position=         20, c1.data=11111111111111111111000000000011 */