Jmeter Randomization Part 4 – RandomFromMultipleVars

If your test needs to randomize values from a static list of values, there are a number of ways to script this.  Jmeter has a new built in function called RandomFromMultipleVars which I have found to be a great time saver.

First create a UDV.  For this example I have created five variables with corresponding static values.  The variable is an animal and the value is a type of animal.

01_udv

Next add a sampler with a JSR223 PreProcessor and paste this code:

String randAnimal = "${__RandomFromMultipleVars(dog|cat|cow|horse|bird)}";
log.info("Random animal type is " + randAnimal);

Please note that there are double quotes around the function call.  This is to ensure that the value is stored as a string.

Open the log viewer and run a few threads.

You should see the value randomized for each thread:

02_log