0
votes

In a thread group, there are more than one http request sampler. I need to send a number with each of these requests. This number should be different for each of these requests. When I used Jmeter Random variable to get a random number every time for every request. What I was thinking that within one thread when I will call this variable for n times within n requests, it will give a different number every time, however it is giving same number in every call.

Please help suggest the way to get a random number every time when this is required within a single thread in different samplers.

5
Please add any relevant code. My guess is that you're calling the random function at the same time and it uses the time as the seed value which means every random number at the same time will be the same.AlexKoren

5 Answers

2
votes

If you need to generate random number within the thread for each sampler, use Random function.

${__Random(1,100)} - will generate a random number between 1 and 100.

2
votes

Here are the options:

  1. __counter() function - returns incremented value each time it's being called.
  2. __time() function - returns current date as timestamp in milliseconds from
  3. __UUID() function - returns unique GUID value
  4. Sometimes JMeter's __Random() function cannot produce real "random" values as if it's called more than once at the same moment (with the same seed) - see point 2 - it will produce 2 equal values. Workarounds are:

1
votes

Personally for what you want I would use the UUID function.

You can try it out by using {__BeanShell(UUID.randomUUID())} in your sampler.

{__BeanShell(UUID.randomUUID())} 

Best of luck and let us know how you get on.

Thanks

0
votes

You must have applied a single random generator in a thread group, if you want to trigger differently for different samplers, you have to add separate random generators for each request.

0
votes

Had a similar issue using Add > Config Element > Random Variable to create a random number. It was being used like so -

log.info("******RandomNumber******");
log.info vars.get("p_randomNumber");

//20%
if(${p_randomNumber} <= 20){

    vars.put("prof_cnt", "010DB");
    vars.put("p_username","${p_prof10}"); 
//30%   
}else if(${p_randomNumber} <= 50){

    vars.put("prof_cnt", "015DB");
    vars.put("p_username","${p_prof15}"); 

What we found is that the JSR223 PreProcessor checkbox "Cache compiled script if available" was checked and that was causing us to get the same user for every pass. Unchecked that box and things worked as planned (got a new user every pass).