How to select a random element from an array.?

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.

@Akhil_Raveendran

Please try this

say array is stored in varArr

then varArr(New Random().Next(0,varArr.Length)) will give random value each time

cheers

2 Likes

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:

  1. Create a list variable in UiPath and populate it with the values you want to choose from.
  2. Use the Count property of the list to get the number of items in the list.
  3. Generate a random number between 0 and the count of the list using the 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.

  1. Use the 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

1 Like

@Akhil_Raveendran
Try this one in assign activity
arr={"a’,“b”,“c”,“d”}
yourVariable = arr(random.nextInt(arr.Length))

1 Like

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.

1 Like
arr = {"a", "b", "c", "d"}
index = New Random().Next(0, arr.Length)
randomElement = arr(index)

Sequence.xaml (5.6 KB)

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.