What is the sympy.randprime() method in Python?
The sympy library
sympy is a library in Python used for symbolic mathematics.
We use pip to install sympy with the following command.
pip install sympy
The randprime method
The randprime method gets a random prime number occurring in the given range. While the starting number is included in the range, the ending number is excluded from the range i.e., [starting_number, ending_number).
Syntax
randprime(a, b)
Parameters
a: This is the starting number of the range.b: This is the ending number of the range.
Return value
The method returns a random prime number in the range [a, b).
Code example
import sympystarting_number = 4ending_number = 100rand_prime_num = sympy.randprime(starting_number, ending_number)print("sympy.randprime(%s, %s) = %s" % (starting_number, ending_number, rand_prime_num))
Code explanation
- Line 1: We import the
sympypackage. - Line 3: We define the starting number of the range.
- Line 4: We define the ending number of the range.
- Line 6: We obtain a random prime number within the range
[starting_number, ending_number)using therandprimemethod.