Array Randomization

Hello, I am trying to randomize an array with the dist keyword, so that I could restrict the elements’s size as well as the weight of each range, so that 80% of the time, I’ll get with in range 20~30, 10% between 60~70 and 10% between 90~100.
But the generated array seems to have bigger values than 100.



class Item #(type DATA_WIDTH = bit [7:0], int arraysize = 10);
    
    static int count;
    int ID;
    rand DATA_WIDTH array [];
    
    function new(); 
        count++;
        ID = count;
    endfunction
    
    function void show();
        $display("Pkt ID %d Data Array :: %p", ID,  array);
    endfunction
                           
    constraint array_c {array.size() == arraysize;
                        foreach (array[i]) array[i] dist {[20:30]:=80, [60:70]:=10, [90:100]:=10};
                        }    
endclass


The generated Array is::
Pkt ID 1 Data Array :: '{87,240,208,133,25,51,204,112,35,156,246,61,207,194,173,243,95,163,48,46,102,221,33,197,129,
81,243,231,47,21,172,176,102,159,151,149,180,67,69,27,226,220,176,151,31,246,178,2,210,98}

In reply to Husni Mahdi:

I tried your code with default specialization across different tools with random seeds , the array elements are always as per constraint .

Could you share a complete code with the class specialization and calls to randomize() ?

In reply to ABD_91:

The top module code ::


`include "Item.sv"

module tb();
    bit clk;
    always #50 clk = ~clk;
    
  Item #(bit [7:0], 10) item;
    
    initial begin
        item = new();
        item.randomize();
        
        item.show();
        item.count_probability();
        
        #50 $finish;
    end

endmodule

In reply to ABD_91:
Altough we are not supposed to talk about any specific softwares here, but the code works on EDAP, but not on Vivado.

Another problem that I am facing is, unique(array) also does not work on vivado (neither Standard nor Enterprise edition), but on EDAP it’s working.

I commented out the ‘item.count_probability’ line (as the code was not shown) and added a repeat loop to run 5 times

This was the output [running in questa]

Pkt ID 1 Data Array :: '{27, 27, 27, 26, 24, 25, 21, 98, 21, 23}

Pkt ID 2 Data Array :: '{27, 93, 20, 23, 23, 94, 62, 30, 96, 24}

Pkt ID 3 Data Array :: '{29, 29, 28, 96, 25, 22, 22, 22, 23, 26}

Pkt ID 4 Data Array :: '{29, 21, 25, 21, 23, 99, 21, 21, 30, 68}

Pkt ID 5 Data Array :: '{60, 20, 24, 30, 26, 25, 29, 99, 20, 27}

** Note: $finish : module.sv(41)

Time: 50 ns Iteration: 0 Instance: /tb

The number of elements in the array is fixed at 10 by the declaration of ‘item’, so above results seemed to be in line with what I would expect.

In reply to graeme_jessiman:

So the code seems OK right??

Also this was the code for the count_probability() function ::


    function void count_probability ();
        int bin_20_30, bin_60_70, bin_90_100;
        
        foreach (array[i]) begin
            if (array[i] inside {[20:30]}) bin_20_30++;
            else if (array[i] inside {[60:70]}) bin_60_70++;
            else if (array[i] inside {[90:100]}) bin_90_100++;
            else if (array[i] > 100) $error("Array elements exceeds limit");
        end
        
      $display("Probability of 20~30 %f", real'(bin_20_30)/(bin_20_30+bin_90_100+bin_60_70));
      $display("Probability of 60~70 %f", real'(bin_60_70)/(bin_20_30+bin_90_100+bin_60_70));
      $display("Probability of 90~100 %f", real'(bin_90_100)/(bin_20_30+bin_90_100+bin_60_70));
        
    endfunction