Last updated: Apr 5, 2024
Reading time·4 min
To generate a random number in a specific range in Postman:
pm.environment.set()
method with lodash.random()
.pm.environment.set( "random_number", _.random(1, 10) )
The code sample generates a random number from 1 to 10 and stores it in the
random_number
key.
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.
{{random_number}}
or {{YOUR_KEY}}
if you've stored the random number in a key with a
different name.{ "id": {{random_number}}, "username": "bobbyhadz{{random_number}}", "password": "dogsandcats123" }
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.
You can also use the random number in your URL as {{random_number}}
.
https://localhost:5000/articles/{{random_number}}
Alternatively, you can use a custom function to generate a random number within a specific range.
pm.environment.set()
method with a custom function.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; }
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.
{{random_number}}
or {{YOUR_KEY}}
if you've stored the random number in a key with a
different name.{ "id": {{random_number}}, "username": "bobbyhadz{{random_number}}", "password": "dogsandcats123" }
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.
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.
{ "id": {{$randomInt}}, "username": "bobbyhadz{{$randomInt}}", "password": "dogsandcats123" }
Here are examples of issuing the HTTP POST request with the specified body.
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.
const random_number = pm.variables.replaceIn( '{{$randomInt}}' ) % 10 + 1; pm.environment.set( "random_number", random_number );
And update your Body tab as follows.
{ "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.
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.
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.
You can learn more about the related topics by checking out the following tutorials: