How to generate random strings or number in Jmeter

When performing load testing in JMeter, generating random strings or numbers is crucial for simulating real-world scenarios. Whether you need dynamic user credentials, unique request parameters, or randomized data to prevent caching issues.

This article will explore different methods to generate random strings and numbers in JMeter.

So, let’s look at all the ways one by one.

Jmeter provides a ${__Random()} function, which we can use to generate random numbers.

${__Random()} takes three arguments

  • Minimum value – required argument
  • Maximum value – required argument
  • Variable name – optional argument

${__Random()} will generate a random number between its minimum and maximum values. So, if we want to generate a random number between 1 and 100, then we’d use ${__Random(1,100)}.

Let’s see it in action. We will execute the https://jsonplaceholder.typicode.com/posts/${postID} where postID will be a random number between 1 and 100.

Set Up a Test Plan
  • Launch JMeter.
  • Go to the “File” Menu and select “New”.
  • This will create a new test plan to be used in our example.
Create a test plan in jmeter
Create a Thread Group
  • Right-click on the Test Plan.
  • To create a Thread group, simply go to “Add > Threads (Users) > Thread Groups”.
Create a Thread group in jmeter

We’ll set the Thread Group to run five tests for now. This will allow us to observe the different random values generated. Check the image below for the configuration details.

Thread group
Add an HTTP sampler

We will use an HTTP sampler to execute our API.

Create an HTTP sampler by right-clicking on the Thread Group and selecting “Add” > “Sampler” > “HTTP Request”.

Create HTTP sampler in jmeter

We will hit the GET request on the “https://jsonplaceholder.typicode.com/posts/${__Random(1,100)}” API where . So, we will add it to the HTTP sampler by following the below steps.

  • Please enter “https” in the input box that is labelled “Protocol[https]:“.
  • Enter “jsonplaceholder.typicode.com” in the input box labelled “Server Name or IP”.
  • By default, the HTTP request value is set to GET. Since we are only executing a GET request, we won’t be changing it.
  • Set the Path to “/posts/${__Random(1,100)}“.
using random function
Add “View Results Tree” listener

View Results Tree listener is one of the most used listeners in Jmeter. It provides detailed information about server responses during performance testing.

Add the “View Results Tree” listener by right-clicking on the HTTP request and selecting “Add” > “Listener” > “View Result Tree”.

View Result Tree
Run Test and monitor results

Run the test case by clicking the green start button and analyze the results in the “view result tree” listener.

As you can see, random numbers between 1 and 100 were generated, and the APIs were executed accordingly using those numbers.

JMeter provides a “Random Variable” config element that allows us to generate random numbers easily.

To add a Random Variable config element in JMeter:

  • Right-click on the Test Plan (or inside a Thread Group where you want to use randomness).
  • Navigate to AddConfig ElementRandom Variable.
"Random Variable" Config

The “Random Variable” config element also includes fields to set a minimum and maximum value. The generated random number is then stored in the variable specified in the “Variable Name” field.

"Random Variable" Config element components

Now, we can use this variable wherever needed. For example, we can reference it in the HTTP Sampler to dynamically insert the generated random number into the HTTP request.

using postid variable in http sampler

Run the Test Plan and check the results. You’ll notice that a random number between 1 and 100 is used in place of the “postID” variable.

Jmeter also provides a ${__RandomString()} function to generate a string randomly.

It accepts below parameters –

  • Length (Required) – A number length of generated String
  • Characters to use ( Optional) – This is the set of characters that will be used to generate the random string.
  • Variable Name ( Optional)

So, ${__RandomString(5,abcdefg)} will generate a random string of length 5 using only the characters “a”, “b”, “c”, “d”, “e”, “f”, and “g”.

Let’s see this in action as well.

We will execute a GET request on https://httpbin.org/get?query=${randomString} API where randomString will be a random string of length 5.

We will use the same thread group where we have set the number of threads as 5. So, let’s directly create the HTTP sampler

HTTP sampler using ${__RandomString()}

Now, let’s run the Test plan and visualize the results in the “View Results Tree” Listener.

As you can see, all the HTTP calls had a randomly generated string of length 5 containing characters from “abcdefg”.

The ${__UUID()} function in JMeter generates a pseudo-random type 4 Universally Unique Identifier (UUID). For example, it might return something like 2fcf430b-20b7-4c2a-a19a-6c912a2e7622.

Let’s use the same test plan and replace ${__RandomString(5, "abcdefg")} with ${__UUID()} to generate a unique identifier instead.

${__UUID} to generate UUID

Run the Test Plan once again.

We hope that you liked the article. If you have any doubts or concerns, please write to us in the comments or mail us at admin@codekru.com.

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *