Multiple random numbers in each interaction

Hi, I want to do a test to my API, it’s a geoip service, what I’m trying to do is generating random numbers, from 1 to 254 (four times) and concat it to get a IP format x.x.x.x, and this should be generated in each interaction.
This is my code actually:

import http from 'k6/http';
import { sleep } from 'k6';
export let options = {
  vus: 100,
  duration: '420s',
};
export default function () {
  http.get('https://api.domain.com/X.X.X.X');
  sleep(1);
}

I would appreciate someone to enlighten me a little where to go.

Sure, you can use the JavaScript function Math.random() to generate as many random numbers as you want.

We also have a small helper function called randomIntBetween() as a part of our utils utility library on jslib.k6.io, which makes generation of random integer numbers a bit easier.

Do you know how can I do for get a random number but for each iteration it changes?

Sure, just make sure to call whichever random function you’re using inside your export default function:

import { randomIntBetween } from 'https://jslib.k6.io/k6-utils/1.2.0/index.js';

export default function () {
  console.log(randomIntBetween(1, 100));
}
1 Like