BreadcrumbHomeResourcesBlog 3 Ways To Generate Random Variables In JMeter August 13, 2021 3 Ways to Generate Random Variables in JMeterOpen Source AutomationBy Leticia AlmeidaSometimes 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.Table of Contents3 Ways to Generate Random Variables in JMeterGo Beyond JMeter Random Variables With BlazeMeterTable of Contents1 - 3 Ways to Generate Random Variables in JMeter2 - Go Beyond JMeter Random Variables With BlazeMeterBack to top3 Ways to Generate Random Variables in JMeterGenerating Random Values from epochEpoch 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: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:And what happens when we run it?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 NowReading the Values from a CSV fileIf 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”.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 ConfigSet the CSV Data Set Config as follows: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}.So, if we run the test, we will see how the script consumes the values defined in the CSV file: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 ElementA third option could be to work with Random Variable, an element available in JMeter’s config elements:Thread group -> Add -> Config Element -> Random VariableThe sampler looks like this: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: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 topGo Beyond JMeter Random Variables With BlazeMeterAfter 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 ResourcesJMeter Performance TestingUsing JMeter Variables With Multiple Thread GroupsBack to top
Leticia Almeida Computer Engineer, Abstracta Inc. Leticia Almeida is a computer engineer and a performance engineer. She has been working at Abstracta for more than 4 years, during which she has gained vast experience in analyzing application performance across multiple technologies. Leticia has leading testing teams and managed performance projects for clients from places all over the world including the USA, Latin America, and Japan.