Search code examples
testingrandomjmeternumbersunique

Generate random numbers that only occur once in JMeter


I want to generate an array of random numbers that only occur once for multiple inputs in JMeter. For example for a range of 1-100:

"age": ${__Random(1,101)}, "weight": ${__Random(1,101)}, "height": ${__Random(1,101)}

There is a chance that two of the variables will have the same value, how could I avoid such incident?


Solution

  • For unique random number you will need to add JSR223 Sampler using ThreadLocalRandom with the following code

    import java.util.concurrent.ThreadLocalRandom;
    int[] array = ThreadLocalRandom.current().ints(0, 100).distinct().limit(3).toArray();
    vars.put("age", String.valueOf(array[0]));
    vars.put("weight", String.valueOf(array[1]));
    vars.put("height", String.valueOf(array[2]));
    

    And then call the parameters in request:

    "age": ${age},
    "weight": ${weight},
    "height": ${height}