Python| sympy.randprime() 方法
借助sympy.randprime()方法,我们可以在[a, b)范围内找到一个随机素数,其中a和b是该方法的参数。
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.
示例 #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))
输出:
A random prime between 4 and 50 is 37
示例 #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))
输出:
A random prime between 60 and 100 is 79