How to select a random element from an array
{"a’,“b”,“c”,“d”}
i just need random element from the above array to a string variable.
How to select a random element from an array
{"a’,“b”,“c”,“d”}
i just need random element from the above array to a string variable.
Please try this
say array is stored in varArr
then varArr(New Random().Next(0,varArr.Length))
will give random value each time
cheers
Hi
Hope the below steps would help you resolve this
To pick a random value from a list variable in UiPath, you can follow these steps:
Count
property of the list to get the number of items in the list.Random
class in C#. You can do this using the Invoke Code
activity and the following code:Dim rnd As New Random() Dim index As Integer = rnd.Next(0, list.Count)
This will generate a random number between 0 and the count of the list, and assign it to the index
variable.
Assign
activity to assign the random value from the list to a new variable using the index
variable as the list index. For example:output = list(index)
This will assign the random value from the list to the output
variable.
Cheers @Akhil_Raveendran
@Akhil_Raveendran
Try this one in assign activity
arr={"a’,“b”,“c”,“d”}
yourVariable = arr(random.nextInt(arr.Length))
in general the main building blocks are given above.
For a better random distribution, we would not use the second parameter for limiting but use the mod function
Assign Activity / Variable panel with Default value:
myRandom | Datatype: Random = new Random()
then use:
arrDemo(myRandom.next() mod arrDemo.length)
Also feel free to incorporate some other diffusions, when it is less random.
arr = {"a", "b", "c", "d"}
index = New Random().Next(0, arr.Length)
randomElement = arr(index)
Sequence.xaml (5.6 KB)
This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.