write a constraint to generate prime numbers in an array between 1 and 200.
In reply to shaiksalim:
https://verificationacademy.com/forums/systemverilog/prime-numbers-constraint
In reply to shaiksalim:
write a constraint to generate prime numbers in an array between 1 and 200.
function void post_randomize();
bit got_num ;
prim_arr.push_back(2);
for(bit [7:0] idx = 3; idx < 200 ; idx ++) begin
got_num = 1;
for(int jdx = 0 ; jdx < prim_arr.size(); jdx++) begin
if(idx % prim_arr[jdx] == 0) begin
got_num = 0;
break;
end
end
if(got_num) begin
prim_arr.push_back(idx);
end
end
//Note: you don't need this. It was for different question.
//tmp_queue = prim_arr.find(item) with (item%10 == 7);
endfunction : post_randomize
class prime_numbers;
rand int unsigned prime;
constraint const_prime {prime.size inside{200};
foreach(prime[i])
if (i>1 && (!((i%2==0 && i!=2) || (i%3==0 && i!=3) || (i%5==0 && i!=5) || (i%7==0 && i!=7))))
prime[i]==i;
else
prime[i]==2;
}
function void post_randomize;
prime = prime.unique();
endfunction
endclass
module tb();
prime_numbers pm;
initial
begin
pm=new();
assert(pm.randomize());
$display(“the prime: %p”,pm.prime);
end
endmodule
I think this may help you.