Distribution constraints

Hello,

In this distribution constraint, I am not getting 50% of time exactly 5 bits are set and remaining 50% of time number of bits set are not 5

/*
Write a constraint in systemverilog
----------------------------
1. In a given array N, 5 bits are set 50% of the time
2. 5 bits are not set remaining 50% of the time
*/


class AC;
  rand bit[9:0] N;
  constraint c1 { N dist { $countones(N)==5 := 50,  $countones(N)!=5 := 50 }; }  
endclass

module TB;
  AC ac;
  
  initial
  begin
    repeat(10)
    begin
      ac = new();
      ac.randomize();
      $display("N is %b", ac.N);
    end
  end
endmodule
N is 0000000000
N is 0000000000
N is 0000000001
N is 0000000000
N is 0000000001
N is 0000000001
N is 0000000000
N is 0000000001
N is 0000000000
N is 0000000001

Is there any problem with the code

Thanks,
JeffD

Try:

  constraint c1 { $countones(N)==5  dist { 1 := 50,  0 := 50 }; }  
1 Like

The problem statement seems awkward. As an example, I think this meets the requirements:

constraint c1 { N dist { 'b11111 := 1, 0 := 1 }; }

But I’m pretty sure it’s not what was intended.

@aaronferrucci, The verbose problem statement is "50% of the time randomize() gets called, 5 bits must be randomly set to 1 out of the 10 bits in variable N. The other 50% of the time, some number of bits other that 5 bits must be set.

1 Like