Random data helps test software with different inputs. It finds hidden bugs by using new values every time.
When you want to test a form with different names or emails.
When you need unique IDs for each test run.
When testing how the system handles unexpected or varied inputs.
When you want to avoid using real user data for privacy reasons.
When you want to simulate real user behavior with changing data.
Syntax
Postman
// Generate a random integer
pm.variables.set('randomInt', Math.floor(Math.random() * 100));
// Generate a random string
pm.variables.set('randomString', Math.random().toString(36).substring(2, 10));
// Generate a random email
pm.variables.set('randomEmail', `user${Math.floor(Math.random() * 10000)}@example.com`);
Use pm.variables.set to save random data for later use in requests.
Random data is generated fresh each time the script runs.
Examples
This sets a variable randNum with a random number from 0 to 50.
Postman
// Random number between 0and50
pm.variables.set('randNum', Math.floor(Math.random() * 51));
This creates a random string with letters and numbers, 6 characters long.
Postman
// Random lowercase string of length 6
pm.variables.set('randStr', Math.random().toString(36).substring(2, 8));
This generates a random email like test123@mail.com for testing signup forms.
Postman
// Random email for testing
pm.variables.set('randEmail', `test${Math.floor(Math.random() * 1000)}@mail.com`);
Sample Program
This script runs before a request in Postman. It creates random user ID, name, and email. Then it saves them as variables to use in the request.
Math.random() * 1000 produces a decimal number like 123.456.
Step 2: Understand email format requirements
Email should not contain decimals in username part; it should be an integer.
Final Answer:
randomNum should be rounded to an integer -> Option B
Quick Check:
Round random numbers for clean strings [OK]
Hint: Round random numbers before string use [OK]
Common Mistakes:
Ignoring decimal values in emails
Misusing pm.environment.set syntax
Not declaring variables properly
5. You want to generate a random user profile in Postman with a name, age (18-60), and email. Which script correctly generates and saves all three as environment variables?