What causes the failure of the randomization process?




 
class test_c;
 
  int  d_array[];
 
  function my_rand();
 
  std::randomize(d_array) with {
    unique{d_array};
    d_array.size() inside {[0:100]};
    d_array.sum() inside {[0:100]};
 
    foreach(d_array[i]) {
      d_array[i] != i;
    }
 
  };
  endfunction
 
 
endclass
 
 
module dut;
 
  initial begin
 
    test_c test;
    test =new();
 
    assert(test.randomize());

    test.my_rand();
    
    foreach(test.d_array[i]) begin

      $display("d_array[%02d]=%02d", i, test.d_array[i]);
    end
    
  end
endmodule    

To check the randomized elements in d_array, I implemented
foreach(test.d_array[i]) begin
$display(“d_array[%02d]=%02d”, i, test.d_array[i]);
end

But I array values are broken. What causes the failure of the randomization process?

Errors: 0, Warnings: 2

vsim dut -batch -do “vsim -voptargs=+acc=npr; run -all; exit” -voptargs=“+acc=npr”

Start time: 05:02:50 on Jun 19,2023

** Note: (vsim-3812) Design is being optimized…

** Warning: testbench.sv(9): (vopt-2240) Treating stand-alone use of function ‘randomize’ as an implicit VOID cast.

** Warning: testbench.sv(7): (vopt-2250) Function “my_rand” has no return value assignment.

** Warning: testbench.sv(34): (vopt-2240) Treating stand-alone use of function ‘my_rand’ as an implicit VOID cast.

** Note: (vsim-12126) Error and warning message counts have been restored: Errors=0, Warnings=3.

// Questa Sim-64

// Version 2021.3 linux_x86_64 Jul 13 2021

//

// Copyright 1991-2021 Mentor Graphics Corporation

// All Rights Reserved.

//

// QuestaSim and its associated documentation contain trade

// secrets and commercial or financial information that are the property of

// Mentor Graphics Corporation and are privileged, confidential,

// and exempt from disclosure under the Freedom of Information Act,

// 5 U.S.C. Section 552. Furthermore, this information

// is prohibited from disclosure under the Trade Secrets Act,

// 18 U.S.C. Section 1905.

//

Loading sv_std.std

Loading work.testbench_sv_unit(fast)

Loading work.dut(fast)

vsim -voptargs=+acc=npr

run -all

d_array[ 0]=-1416642294

d_array[ 1]=1717110569

d_array[ 2]=1063457890

d_array[ 3]=-322822339

d_array[ 4]=2030514636

d_array[ 5]=-1822465458

d_array[ 6]=-1973586523

d_array[ 7]=1816285422

d_array[ 8]=-695846032

d_array[ 9]=-2083486058

d_array[10]=-1297384948

d_array[11]=-721885058

d_array[12]=917887356

d_array[13]=-543104313

d_array[14]=1234606402

d_array[15]=-782592491

d_array[16]=949798174

d_array[17]=1930155088

exit

End time: 05:02:50 on Jun 19,2023, Elapsed time: 0:00:00

Errors: 0, Warnings: 3

In reply to UVM_LOVE:

What makes you think that the randomize() call failed? You are using signed 32-bit integers. If you add all the numbers and account for overflow, you will get a value between 0 and 100.

If you want each number to be between 0 and 100, you should add another constraint, although it is impossible to get 100 unique integers between 0 and 100 to sum to a total between 0 and 100.

In reply to cgales:

Could you give me an example for “If you add all the numbers and account for overflow,” I want each number to be between 0 and 100, but don’t know what I should add another constraint. What I need to add more for the constraint?

In reply to UVM_LOVE:


class test_c;
  int d_array[];
 
  function int my_rand();
    return std::randomize(d_array) with {
      unique{d_array};
      d_array.size() inside {[0:100]};
      d_array.sum() inside {[0:10000]};
      foreach(d_array[i]) {
        d_array[i] inside {[0:100]};
        d_array[i] != i;
      }
    };
  endfunction
  
  function void print();
    foreach(d_array[i])
      $display("d_array[%02d]=%02d", i, d_array[i]);
  endfunction
endclass
 
 
module testbench;
  test_c test;
  initial begin
    test =new();
    if (!test.my_rand()) $display("Randomization failed!");
    else test.print();
  end
endmodule