❔ Need help selecting a random element out of a list

Hey I'm very new to learning C# and I'm trying to figure out how I would select a random element from a list. I am working with Selenium browser automation and I have a list of potential elements that I want the browser to interact with. I essentially want one element from the list to be selected to be clicked on each time the program runs. It seems like there should be a more elegant solution than creating an IF, ELSE IF list with 22 entries for the 22 potential variables that could be selected. Is there a way to simply choose an item from a list and append the .click command onto the variable that has been selected?
4 Replies
Becquerel
Becquerel14mo ago
var item = yourList[Random.Shared(yourList.Count - 1)] if Random.Shared doesn't work, use new Random()
Grip It and Rip It
woot thank you!
TheRanger
TheRanger14mo ago
it should be var item = yourList[Random.Shared.Next(yourList.Count)]; maxValue parameter is exclusive upper bound
Accord
Accord14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts