Randomizing the prime numbers

here is the code for prime number using constraints…

class array;
rand int array[$];
constraint const_array{array.size inside {100};
	   foreach(array[i])
 if(!((i%2==0 && i!=2) || (i%3==0 && i!=3)  || (i%4==0 && i!=4) || (i%5==0 && i!=5) || (i%6==0 && i!=6) || (i%7==0 && i!=7) || (i%8==0 && i!=8) || (i%9==0 && i!=9)))
                 array[i]==i;
            else
		 array[i]==1;}

		           
function void disp();
$display("the array: %p",array);
endfunction
endclass

output:

the array: '{1, 1, 2, 3, 1, 5, 1, 7, 1, 1, 1, 11, 1, 13, 1, 1, 1, 17, 1, 19, 1, 1, 1, 23, 1, 1, 1, 1, 1, 29, 1, 31, 1, 1, 1, 1, 1, 37, 1, 1, 1, 41, 1, 43, 1, 1, 1, 47, 1, 1, 1, 1, 1, 53, 1, 1, 1, 1, 1, 59, 1, 61, 1, 1, 1, 1, 1, 67, 1, 1, 1, 71, 1, 73, 1, 1, 1, 1, 1, 79, 1, 1, 1, 83, 1, 1, 1, 1, 1, 89, 1, 1, 1, 1, 1, 1, 1, 97, 1, 1}

here in the output,the value one is constrained for the rest of the non prime index of the array…i.e array[0]=1;array[1]=1;array[2]=2;array[3]=3;array[4]=1;…so on

how to find the prime numbers from 0 to 100;???
without wasting memory in the array…

expected output:
2,3,5,7,11,13,17,19,23,29,31,…89,97;

1 Like

In reply to pawan_101010:
Unless someone comes up with a function that can tell you how many prime numbers there are in a range of numbers, you can’t do this with a constraint. The size of an array must be solved before applying constraints to the individual elements.
You can strip the array with the unique() method

function void post_randomize;
   array = array.unique();
endfunction

solution…
to find prime numbers…

class prime_numbers;
   parameter RANGE=1000;// this will give all prime number with in 1000, we can give any range here.......!!!

   rand int unsigned prime[$];
   constraint const_prime {prime.size inside {RANGE}; 
                              foreach(prime[i])
                                 if (i>1 && (!((i%2==0 && i!=2) || (i%3==0 && i!=3)  || (i%4==0 && i!=4) || (i%5==0 && i!=5) || (i%6==0 && i!=6) || (i%7==0 && i!=7) || (i%8==0 && i!=8) || (i%9==0 && i!=9))))
                                     prime[i]==i;
                                 else
                                      prime[i]==2;
                          }

    function void post_randomize;
      prime = prime.unique();
    endfunction

endclass

program top;
  prime_numbers arr;

  initial
    begin
     arr=new();
     assert(arr.randomize());
     $display("the prime: %p",arr.prime);
    end
endprogram 

output:::

# the prime: '{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 121, 127, 131, 137, 139, 143, 149, 151, 157, 163, 167, 169, 173, 179, 181, 187, 191, 193, 197, 199, 209, 211, 221, 223, 227, 229, 233, 239, 241, 247, 251, 253, 257, 263, 269, 271, 277, 281, 283, 289, 293, 299, 307, 311, 313, 317, 319, 323, 331, 337, 341, 347, 349, 353, 359, 361, 367, 373, 377, 379, 383, 389, 391, 397, 401, 403, 407, 409, 419, 421, 431, 433, 437, 439, 443, 449, 451, 457, 461, 463, 467, 473, 479, 481, 487, 491, 493, 499, 503, 509, 517, 521, 523, 527, 529, 533, 541, 547, 551, 557, 559, 563, 569, 571, 577, 583, 587, 589, 593, 599, 601, 607, 611, 613, 617, 619, 629, 631, 641, 643, 647, 649, 653, 659, 661, 667, 671, 673, 677, 683, 689, 691, 697, 701, 703, 709, 713, 719, 727, 731, 733, 737, 739, 743, 751, 757, 761, 767, 769, 773, 779, 781, 787, 793, 797, 799, 803, 809, 811, 817, 821, 823, 827, 829, 839, 841, 851, 853, 857, 859, 863, 869, 871, 877, 881, 883, 887, 893, 899, 901, 907, 911, 913, 919, 923, 929, 937, 941, 943, 947, 949, 953, 961, 967, 971, 977, 979, 983, 989, 991, 997}



tq daveeeee

In reply to pawan_101010:

FYI, Your constraint does not work for prime numbers greater than 100. e.g. 121 and 989 are not prime numbers.

ya

In reply to pawan_101010:

Try below code.

In reply to kranthi445:
Now you are no longer using the constraint solver.

Please find the below snippet for generation of prime numbers.


module test();
 
  class prime_num;
    randc int prime;
    rand int temp[];
   
    constraint temp_size {temp.size == (prime/2)-1;}
    constraint temp_val {foreach(temp[i])
                            temp[i] == i+2;}
    constraint number {foreach(temp[i])
                           if (prime !=2)
                               prime%temp[i] != 0;}
    constraint range {prime inside {[1:100]};}
  endclass
 
  prime_num num;
 
  initial
    begin
      num = new();
      repeat(20)
        begin
            if(!num.randomize())
            begin
              $display("Randmoization failed");
              $finish;
            end
          $display("number generated is %0d", num.prime);
        end
    end
endmodule

The constraints that I have written are working fine in cadence incisive but, constraint solver is being failed while using synopsis and aldec tools. I didnt try in questa.

Ofcourse, I am well aware of the fact that, this forum is not for having a discussion on eda tools.

isn’t finding prime number a suitable candidate for a procedural code than constraints…??
isn’t it better to put it inside pre_randomize from performance perspective ?

dave_59, any thoughts ??

In reply to ssureshg_:

Most likely an interview question

class packet;
  rand int arr[];
  
  ///////////Constraint
  constraint cons_name2{arr.size() inside{[10:25]};}
  constraint cons_name{foreach(arr[index])
 						 {
                           arr[index] == prime(index);
                         }
                      }
  
  //////////properties for prime num
   int prime_num=7; //last saved prime number is 7 hence 8
  
   
  ///////////Method for prime num
    function int prime(int index);
       int flag=0; //default number is not prime number assumption hence flag is 0
         //hard code first 4 element for 6n-1 and 6n+1 rule              
         if(index == 0)
     	 	return 2;
         else if(index == 1)
     		return 3;
         else if(index == 2)
     		return 5;
         else if(index == 3)
     		return 7;
         else begin
      
         prime_num= prime_num +1;
           
      
      for(int i=2; i<=(prime_num)/2;i++)begin
        if(prime_num%i == 0)begin //any divisor found able to divide our prime_num
          flag = 0;     //means prime_num is not prime hence need to update prime_num and start loop again 
          prime_num = prime_num +1; //increment number
          i=2; //reset base divisor to 2
        end 
        else begin
          flag = 1; 
        end 
      end
      
           
      if( flag == 1 ) begin
        return prime_num;
      end
           
     end   
    endfunction
                                             
                              
 endclass
                      
                       
module TB_TOP;
  
  initial begin
    packet pkt=new();
    pkt.randomize();
    $display("Prime no in array %p",pkt.arr);
  end

endmodule

In post randomize function we can shuffle array to get random prime numbers in array :)