I would like to create a dynamic variable - a random 10-digit number
how do i do this?
Hi,
A simple way to do it, is to use the built-in library lodash.
e.g.
const randomNumber = _.random(1000000000,9999999999)
If you need to use it in a request, you can add this to a pre-request script like this:
const randomNumber = _.random(1000000000,9999999999)
pm.variables.set(‘randomNumber’, randomNumber )
then in the request body, add {{randomNumber}}
e.g.
{
"username": "Test",
" number": "{{randomNumber}}"
}
I hope this helps.
amazing
thank you very much!!
Your question may already have an answer on the community forum. Please search for related topics, and then read through the guidelines before creating a new topic.
Here’s an outline with best practices for making your inquiry.
My question:
Details (like screenshots):
How I found the problem:
I’ve already tried:
Update:
With lodash being deprecated from the built-in libraries, you must load it first.
const {random} = require(‘lodash’)
const randomNumber = random(1000000000,9999999999)
pm.variables.set(‘randomNumber’, randomNumber )
or use native javascript like this:
function generateRandomNumber() {
const min = 1000000000; // Minimum value
const max = 9999999999; // Maximum value
const randomNumber = Math.floor(Math.random() * (max - min + 1)) + min;
return randomNumber;
}
const randomNumber = generateRandomNumber();
console.log(randomNumber);