This does have consecutive zeroes as you wish to have .
Also solve byte_en_max_10 before SUM isn’t correct .
**solve a before b is used when a and b are random variables and not constraint Names .
**
I don’t get your statement " .I have to generate only 10-1’s out of 32 bit value "
// The below code will help you
class A;
rand bit [31:0] b;
bit [31:0] bQ[$:10];
constraint SUM {
foreach(b[i])
if ( i < 31 ) // Else Error :: " Constraint solver out of bound error "
soft int'( b[i] + b[i+1] ) == 2'd1 ;
}
constraint max_10 {
$countones(b) == 10;
unique{b,bQ};
}
function void post_randomize();
bQ.push_back(b);
endfunction
endclass
module top;
A a=new;
initial begin
for(int i=0;i<9;i++)begin
a.randomize();
$display("value of a = %b",a.b);
end
end
endmodule
initial
begin
gen_random g_h;
g_h = new();
if(!g_h.randomize())
$display("FAILED To RANDOMIZE");
else
$display("Random Number %0b",g_h.junk_number);
end
endmodule
This code is not giving any out of bound errors. It directly generates the required random number.
No need for soft constraint or additional array or post randomize logic.
class A;
rand bit [31:0] b;
constraint cnt{
$countones(b) == 10;
foreach(b[i]){
if(i>0) (b[i] & b[i-1]) == 0 ;
}
}
endclass
module top;
A a=new;
initial begin
for(int i=0;i<9;i++)begin
a.randomize();
$display("value of a = %b",a.b);
end
end
endmodule