Skip to content
Related Articles

Related Articles

Improve Article

Python | sympy.randprime() method

  • Last Updated : 27 Aug, 2019

With the help of sympy.randprime() method, we can find a random prime number in the range [a, b), where a and b are parameters to the method.

Syntax: randprime(a, b)

Parameter:
a – It denotes the start of the range. It is inclusive.
b – It denotes the end of the range. It is not inclusive.

Returns: Returns a random prime in the given range. If no prime is present in the range then it raises a ValueError.

Example #1:






# import randprime() method from sympy
from sympy import randprime
  
a = 4
b = 50
  
# Use randprime() method 
a_randprime_b = randprime(a, b) 
      
print("A random prime between {} and {} is {}".format(a, b, a_randprime_b))

Output:

A random prime between 4 and 50 is 37

Example #2:




# import randprime() method from sympy
from sympy import randprime
  
a = 60
b = 100
  
# Use randprime() method 
a_randprime_b = randprime(a, b) 
      
print("A random prime between {} and {} is {}".format(a, b, a_randprime_b))          

Output:

A random prime between 60 and 100 is 79

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :