SV Proability Constraint for Number of Set Bits

Write a constraint for a 10 bit variable en so that; → 10% of the time 1 bit in en is high → 10% of the time 2 bits in en are high … → 10% of the time all 10 bits in en are high.

class sample;
  
  rand bit [10:1] en;
  
  constraint x{
    foreach(en[i])
    {
      $countones(en) == i dist {1:=10,0:=90};
    }
    
  }
  
endclass

Is my solution correct for this problem?

You can try this:

constraint x {
    $countones(en) dist {[1:10]:=1};
}
3 Likes

Shouldn’t it be

constraint x {
    $countones(en) dist {[1:10]:=10};
}
class first;
  rand bit [9:0] en; 
  randc int b;
  constraint b_1 {
    			b >= 1;
  				b <=10;
    
  				}
  
  constraint en_1 {
    $countones(en) == b;
  				}
   
endclass
1 Like

I’m not sure if this a real good example for randomization.
I guess you want to see something like this

1 Like