Prime numbers constraint

write a constraint to randomly generate 10 unique prime numbers in an array between 1 and 200. the generated prime numbers shoould have 7 in it . example ( 07,17,37… et

In reply to pradeepD:

What is your question?

In reply to dave_59:

My question is that how I will make that lsb part as constant 7

In reply to pradeepD:

You could simply constraint lower 4 bits to be 7


constraint LSB_7 { rand_var[3:0] == 7 ; }

In reply to ABD_91:

I am not able to understand your point. My point is that the randomly generated prime number should have 7 at the lsb side (eg 17,37,47 etc…).

In reply to pradeepD:

You want the numbers to be BCD ( 17 , 37 , 47 etc … )

So each digit would be represented in 4-bits .

Your question is how do I make LSB part as 7 , I gave you the solution to achieve it

The lowest digit is always 7 by constraint LSB_7

In reply to ABD_91:

Thanks Dave and ABD_91 for the clarification

In reply to pradeepD:


module test;
class prime_number;
  rand bit [8:0] a[$],b[$];
  constraint abc {a.size==200; }
  constraint cba { foreach(a[i])
    if(i>1 )
     a[i]==prime(i);}
 
  function int prime( int g);
    for(int i=2;i<g;i++)
        if(g%i==0)
          return 2; //if it is not a prime number ,returning 2 which is one of prime
      prime=g;
    endfunction
  function void post_randomize();
    a=a.unique;
   for(int i=0;i<a.size;i++)
     if(a[i]%10==7)
       b.push_back(a[i]);  //in b queue you will find prime numbers with units place as 7.
 
  endfunction
endclass

prime_number pri;

initial
  begin
    pri=new;
    void'(pri.randomize);
    $display("%p",pri);
  end
endmodule
1 Like

In reply to Bandaru sateesh:

Hi, I have run your code on eda playground platform. It’s giving some unexpected outcomes. In queue a, we are saving all prime numbers less than 200. But we didn’t mention any specific condition for the first two cases when i=0 and i=1. Due to this reason, we are getting some absurd values which are not satiating our requirement partially.
I tried to redesign the code. Please look into it. If there is any edge case, I missed out on, please mention it in this thread.

module test;
class prime_number;
  rand int a[],b[$];
  constraint abc {a.size==200; }
  constraint cba { foreach(a[i])
    a[i]==prime(i);}
 
  function int prime( int g);
    if(g <= 1)return 2;
    for(int i=2;i<g;i++)
      if(g%i==0)
          return 2; //if it is not a prime number ,returning 2 which is one of prime
      prime=g;
    endfunction
  function void post_randomize();
    a=a.unique;
   for(int i=0;i<a.size;i++)
     begin
     if(a[i]%10==7)
       b.push_back(a[i]);  //in b queue you will find prime numbers with units place as 7.
    if(b.size() == 10) break;
     end
  endfunction
endclass
 
prime_number pri;
 
initial
  begin
    pri=new;
    assert(pri.randomize);
    $display("%0p",pri.b);
  end
endmodule

In reply to Shubhabrata:

Hi, I just found out this thread. My logic was mostly similar except I wasn’t using the
prime = g logic. I’m not entirely sure what/how does that work? Are you assigning value to a variable? Where are you returning that? I’m unfamiliar with this syntax of system verilog function.

Thanks

In reply to nitin4natu:

Verilog allows you to use the name of the function as an implicit variable storing the return value. If you exit the function without using return, that value becomes the returned value. In this case, prime=g; is the same as return g;

Note that prior to SystemVerilog, the return construct did not exist. so you had to make an assignment to the implicit variable.

*In reply to dave_59:*Thanks, Dave!

This is my solution. Seems to work fine, if you guys see any issues, please let me know.

module test();

  class x;
    rand int arr;
    int q[$];

    constraint sel{
      !(arr inside {q});
      arr < 200;
      arr>0; 
      arr%10 == 7;


    }

    function int prime(int arr);
      for(int i = 2;i<arr;i++) begin
        if(arr%i==0)
          return 0;
      end
      return 1;
    endfunction


    function void post_randomize();

      if(prime(arr))
        q.push_back(arr);
    endfunction
  endclass

  initial begin
    x a = new();
    while(a.q.size()<10) begin
      a.randomize();
    end
    $display("%p",a.q);
  end
endmodule