Random number distribution

Hi,

I have a random array value and I want to use the dist keyword on the sum. Does the below code work?


rand bit[3:0] arr[];

constraint c {
    arr.size()==510;
    arr.sum with (int'(item)) dist {[1:50]:/90,[51:510]:/10};
}

Here I am trying to get array elements such that the sum of the elements is less than 50 90% of the time.

Thanks

In reply to sj1992:

The way to check is randomizing this 1000 times and checking how many times the sum is less than 50.

P.S. Did you mean to write [0:49] instead of [1:50]?

In reply to dave_59:

Hi Dave, I randomized the array 100 times, and when I saw the value for the sum its always 50 or 510. Can you explain why the sum is only 50 or 510?


// This is the class that we will randomize.
class my_item;
 
  rand bit [2:0] rand_val;
  rand bit[3:0] arr[];

  // Randomization constraints.
  constraint c {

    arr.size()==510;
    arr.sum with (int'(item)) dist {[1:50]:/90,[51:510]:/10};
    //rand_val dist {0:/10 , 1 :/ 45 , 2 :/ 45};
  }
  
  // Print out the items.
  function void print();
    //$display("rand_val: %0d", rand_val);
    $display("arr_sum: %0d", arr.sum with (int'(item)));
  endfunction
  
endclass

module automatic test;
  
  function void run;
    my_item item = new();
    for (int i = 0; i < 200; i++) begin
      item.randomize();
      item.print();
    end
    
  endfunction
  
  initial run();
  
endmodule

In reply to sj1992:

Works for me. You might have a tool issue. This Mentor/Siemens EDA sponsored public forum is not for discussing tool specific usage or issues. Please read your tool’s user manual or contact your tool vendor directly for support.