Generate a random number in a specific range in Postman

avatar
Borislav Hadzhiev

Last updated: Apr 5, 2024
4 min

banner

# Table of Contents

  1. Generate a random number in a specific range in Postman
  2. Generate a random number in a specific range with a custom function
  3. Using the $randomInt Postman variable to generate a random number

# Generate a random number in a specific range in Postman

To generate a random number in a specific range in Postman:

  1. Click on the Pre-request Script tab and use the pm.environment.set() method with lodash.random().
pre-request-script.js
pm.environment.set( "random_number", _.random(1, 10) )

generate random number in postman using lodash

The code sample generates a random number from 1 to 10 and stores it in the random_number key.

The lodash.random() method generates a random number between the inclusive lower and upper bounds.

If the method is only called with 1 number, a random number between 0 and the given number is returned.

  1. Click on the Body tab and user the random number as {{random_number}} or {{YOUR_KEY}} if you've stored the random number in a key with a different name.
body.js
{ "id": {{random_number}}, "username": "bobbyhadz{{random_number}}", "password": "dogsandcats123" }

use random number in body tab

The example assumes that you're sending an HTTP request with a JSON body.

Notice that I've selected raw from the dropdown menu and then JSON.

The clip below shows how this works when issuing HTTP POST requests to an API endpoint that responds with the key-value pairs from the body object.

generate random number in range in postman using lodash

You can also use the random number in your URL as {{random_number}}.

url
https://localhost:5000/articles/{{random_number}}

# Generate a random number in a specific range with a custom function

Alternatively, you can use a custom function to generate a random number within a specific range.

  1. Click on the Pre-request Script tab and use the pm.environment.set() method with a custom function.
pre-request-script.js
pm.environment.set( "random_number", getRandomNumber(1, 10) ) // min and max are both inclusive function getRandomNumber(min, max) { const minNumber = Math.ceil(min); const maxNumber = Math.floor(max); return Math.floor( Math.random() * (maxNumber - minNumber + 1) ) + minNumber; }

generate random number in range using custom function

The example generates a random number between 1 and 10.

Note that the min and max values are both inclusive.

In other words, the random number could be 1 and it could also be 10.

  1. Click on the Body tab and user the random number as {{random_number}} or {{YOUR_KEY}} if you've stored the random number in a key with a different name.
body.js
{ "id": {{random_number}}, "username": "bobbyhadz{{random_number}}", "password": "dogsandcats123" }

use random number in body tab

The example assumes that you're sending an HTTP request with a JSON body.

Notice that I've selected raw from the dropdown menu and then JSON.

# Using the $randomInt Postman variable to generate a random number

Postman also exposes a $randomInt dynamic variable that can be used to generate a random integer between 0 and 1000.

For example, I'll remove the Pre-request Script and update the request Body as follows.

body.js
{ "id": {{$randomInt}}, "username": "bobbyhadz{{$randomInt}}", "password": "dogsandcats123" }

generate random number using random int

Here are examples of issuing the HTTP POST request with the specified body.

generate random number using random int

Notice that the random number that is generated for the id property differs from the random number that is generated for the username property.

If you want to generate a random number in a specific range, use the modulo (%) operator.

The following example generates a random number from 1 to 10 using the built-in $randomInt environment variable.

Update your Pre-request script as follows.

pre-request-script.js
const random_number = pm.variables.replaceIn( '{{$randomInt}}' ) % 10 + 1; pm.environment.set( "random_number", random_number );

generate random number in range using random int

And update your Body tab as follows.

body.js
{ "id": {{random_number}}, "username": "bobbyhadz{{random_number}}", "password": "dogsandcats123" }

Here is a short clip of issuing POST requests with the randomly generated number from 1 to 10.

issuing post requests with random number in range

You can adjust the number to the right of the module operator if you want to generate a random number in a different range.

The following example generates a random number from 1 to 5.

pre-request-script.js
const random_number = pm.variables.replaceIn( '{{$randomInt}}' ) % 5 + 1; pm.environment.set( "random_number", random_number );

The start and end values are inclusive. In other words, the number could be 1 or 5.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.