Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
communitycreator
sympy

What is the sympy.randprime() method in Python?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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 sympy
starting_number = 4
ending_number = 100
rand_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 sympy package.
  • 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 the randprime method.

RELATED TAGS

python
communitycreator
sympy

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring