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.
random. choice() function is used to get random elements from a NumPy array.
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).
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.
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,)})}"
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With