Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including random-prime with all npm packages installed. Try it out:

var randomPrime = require("random-prime")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

random-prime v1.0.0

Generate a random prime number

random-prime

Generate a random prime number

This uses Math.random internally.

Install

npm i random-prime --save

Example

const randomPrime = require('random-prime').randomPrime;

console.log(randomPrime());
// 254205915209711
console.log(randomPrime(500));
// 119
console.log(randomPrime(200, 800));
// 413

API

randomPrime()

Generate a random prime number from 0 to Number.MAX_SAFE_INTEGER.

Returns a prime number

randomPrime(max)

Generate a random prime number from 0 to max.

Returns a prime number or null if max < 2
Throws TypeError if max is not a Number

randomPrime(min, max)

Generate a random prime number from min to max.

Returns a prime number or null if there is no prime number between min and max
Throws TypeError if min and/or max is not a Number

isPrime(num)

An efficient method to check i a number is prime.

Returns true if num is prime, false if num is not prime.
Throws TypeError if input is not a Number

Example

const isPrime = require('random-prime').isPrime;

console.log(isPrime(2));
// true
console.log(isPrime(254205915209711));
// true
console.log(randomPrime(500));
// false
console.log(randomPrime(-10));
// false

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free