RANDPRIME

This function returns a prime selected from the interval [a, b).

The sampled value is constrained by primality and interval bounds:

p \in [a, b), \quad p \text{ is prime}

A fixed random seed is used to make results reproducible across test runs.

Excel Usage

=RANDPRIME(a, b)
  • a (int, required): Inclusive lower bound.
  • b (int, required): Exclusive upper bound.

Returns (int): A prime number sampled from the requested range.

Example 1: Sample prime from one to thirty

Inputs:

a b
1 30

Excel formula:

=RANDPRIME(1, 30)

Expected output:

29

Example 2: Sample prime from fifty to one hundred

Inputs:

a b
50 100

Excel formula:

=RANDPRIME(50, 100)

Expected output:

97

Example 3: Sample prime from one hundred to one twenty

Inputs:

a b
100 120

Excel formula:

=RANDPRIME(100, 120)

Expected output:

113

Example 4: Sample prime using Bertrand-style interval

Inputs:

a b
30 60

Excel formula:

=RANDPRIME(30, 60)

Expected output:

37

Python Code

import random
from sympy import randprime as sympy_randprime

def randprime(a, b):
    """
    Sample a prime number from a half-open integer interval.

    See: https://docs.sympy.org/latest/modules/ntheory.html

    This example function is provided as-is without any representation of accuracy.

    Args:
        a (int): Inclusive lower bound.
        b (int): Exclusive upper bound.

    Returns:
        int: A prime number sampled from the requested range.
    """
    try:
        random.seed(0)
        result = sympy_randprime(a, b)
        return int(result)
    except Exception as e:
        return f"Error: {str(e)}"

Online Calculator

Inclusive lower bound.
Exclusive upper bound.