postman - how to generate random number in specifi

2020-07-02 09:41发布

问题:

I need to generate random number in specific range for postman. I wonder if there is possible to generate it and to use it in variable in postman, from what I saw at postman site is:

{{$randomint }}

will give random number between 1-1000 and I already tried to do something like this:

{{$randomint(1,5)}}

to get number between 1-5 but postman didn't get this kind of option, so how to specify range for the random in postman?

回答1:

You can just use Lodash for this as it's a built-in module:

pm.environment.set("random_number", _.random(1, 5))

Or just add the number 5 to the _.random() function and this will be a number from 0 to 5.



回答2:

This worked for me:

In your Pre-request script define your variable with:

pm.globals.set('randomNumber', Math.floor(Math.random() * 5));

Then in your URL call your variable in the URL like so:

{{randomNumber}}

Hope this works for you.