3 Ways To Generate Random Variables in JMeter
August 13, 2021

3 Ways to Generate Random Variables in JMeter

Open Source Automation

Sometimes we need to generate a JMeter random variable in our scripts and then work with them in our requests. For example, in cases where the URL requires an auto-generated and unique value as a parameter.

There are many ways to do this in JMeter. In this blog, we will review some of the most commons ones.

Back to top

3 Ways to Generate Random Variables in JMeter

Generating Random Values from epoch

Epoch time, also known as Unix epoch time, is a string of numbers that represents a specific and unique time (the number of seconds that have elapsed since January 1, 1970).

Working with this value helps you to simulate the request in an easy way, when the URL you want to hit looks like this and you need to randomize numbers for it:

jmeter, random variables

In these cases, when setting up your request, you just have to invoke ${__time()}. This way you can generate a random and unique value.

Let see how to look at this invoke in JMeter:

load testing with random variables

And what happens when we run it?

jmeter variables

As you can see, this JMeter function generated requests with a random and unique value for each of my calls.

That was easy, right? Ok, let’s continue reviewing other options.

Start testing with Random Variables today with BlazeMeter!

Start Testing Now

Reading the Values from a CSV file

If you have already generated the values you want to use, you can list them in a CSV file and consume them in your requests. But, how can I achieve this? Let’s see.

Imagine we have a list of random variables that are already generated, saved as “variables.csv”.

how to get random variables from jmeter

Now, we want to consume this list in our script. So, we have to add a CSV Data set config as we show below.

Thread Group -> Add -> Config Element -> CSV Data Set Config

variables, jmeter, csv file, csv data set config

Set the CSV Data Set Config as follows:

jmeter, csv data set config configuration

  • Filename: The name of the file, in this case. Please notice that if you locate the file in the same folder as the script, you just have to enter the name. In this case the full path is not necessary, and you can work with the relative path of the script.
  • Variable names: The name we are going to use to invoke the variable. In this example we set it as “random”.
  • Leave the rest of the fields as their default settings. It is important to keep the Sharing mode as “All threads” if you don’t want to repeat values.

If you want more information about this element sampler, please check it out here on Apache JMeter.

Having done this, we just have to invoke the value typing ${random}.

invoking variables in jmeter

So, if we run the test, we will see how the script consumes the values defined in the CSV file:

load testing with jmeter tutorial

So, this is another option to work with random variables, in cases in which you already have the list of random variables you want to use.

Working with the Random Variable Element

A third option could be to work with Random Variable, an element available in JMeter’s config elements:

Thread group -> Add -> Config Element -> Random Variable

jmeter, random variable element

The sampler looks like this:

jmeter , load testing, random variable sampler

Setting it as follows:

  • Variable name: The name we are going to use to invoke the variable. “random” in this example.
  • Output Format: The format for the variable. You can set the desired length of the number. I've set 00000 in order to work with a five-digit number. You can also use USER_000.
  • Minimum and Maximum: The range we want to set for the variable.
  • Seed: The seed for the random number generator. A seed is the first input that the number generation function receives to start the random generation.
  • Per Thread: Is important to take this option into consideration. If it is ‘False’, then the generated value will be shared between all threads in the thread group. If it is ‘True’, then each thread has its own value.

Please, check JMeter’s component reference to read more information about the Random Variable.

So, if we run the test now, we will see:

jmeter performance testing

We have shown three different options we can follow to achieve a script that works with random variables each time. Choose the option that fits you best according to your needs. If you have more ideas, please share them in the comments section below!

Back to top

Go Beyond JMeter Random Variables With BlazeMeter

After creating your JMeter script with its random variables, run it in BlazeMeter. You will be able to massively scale, share tests and results and analyze metrics in real-time or compare them over time.

START TESTING NOW

 

Related Resources

Back to top