Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get random values from an array

Tags:

jmeter

I defined a new variable

Name        Value            Description
categories  (1, 2, 3, 4, 5)  my categories ids

and in my path i want to get a random value from categories: category_id=my_random_value.

I even tried this

category_id=${__StringFromArrayAtRandomindex('1', '2', '3', '4', '5'}

but it doesn't work.

like image 556
Mohamed Omezzine Avatar asked Feb 04 '13 20:02

Mohamed Omezzine


People also ask

How do you generate a random value from an array in Python?

random. choice() function is used to get random elements from a NumPy array.

How can you pick a random item from a range?

Use a random.randrange() function to get a random integer number from the given exclusive range by specifying the increment. For example, random.randrange(0, 10, 2) will return any random number between 0 and 20 (like 0, 2, 4, 6, 8).

How do you select a random string from an array?

We can use the random number generator to pick a random item from an array. The following code snippet has an array of author names (strings). We can pick a random author by generating a random number that is less than the number of items in the array and use the random index to pick a random author name in the string.


1 Answers

To obtain a random variable value from a list, first declare as User variables the list or available values, with a prefix and a incremental index:

country_1     Spain 
country_2     France  
country_3     Portugal  
country_4     Italy 
country_5     England

Then you can obtain a random value from the list concatenating the prefix with a random index in the interval:

${__V(country_${__Random(1,6,)})}  --> "Spain", "France", "Portugal", etc...

Explanation

The __Random function will give you an index for your interval. To obtain values from 1 to 5, you have to call __Random(1,6,), as it will never reach the MAX value.

The __V function, will obtain the value of the variable with the given name.

${__Random(1,6,)}                 --> 1, 2, 3, 4, 5
country_${__Random(1,6,)}         --> "country_1", "country_2", etc...
${__V(country_${__Random(1,6,)})} --> "Spain", "France", "Portugal", etc...

As example, to use the random variable as JSON body for a request, in Body Data:

}
  "country":"${__V(country_${__Random(1,6,)})}"
}
like image 190
AlexGuti Avatar answered Sep 20 '22 07:09

AlexGuti