1
votes

I've got a problem with trying to randomize some values. In this case I'm using Random to select a title from a string list.

import java.util.Random;
titles = "Mr,Mrs,Sir,Ms,Dr";
String[] titles = titles.split(",");
Random random = new Random();
String randomValue = titles[random.nextInt(titles.length)];
vars.put("TITLE", randomValue);

Above works fine for a single run of a thread but if you're looping then it stops generating new values. If I run multiple threads then it generates different values.

1
Add actual and expected resultsuser7294900

1 Answers

1
votes

My expectation is that your Beanshell script fails somewhere somehow, most probably you're facing a race condition when trying to overwrite titles variable value by something which changes its type.

You can amend your code to look like:

import java.util.Random;
titles = "Mr,Mrs,Sir,Ms,Dr";
String[] titles2 = titles.split(",");
Random random = new Random();
String randomValue = titles2[random.nextInt(titles2.length)];
vars.put("TITLE", randomValue);

and it should resolve your issue.

A better solution would be migrating to a JSR223 Test Element and Groovy language as it's recommended to do so since JMeter 3.1, Groovy has better performance comparing to Beanshell hence you should be able to re-use your existing code.