❔ 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
var item = yourList[Random.Shared(yourList.Count - 1)]
if Random.Shared doesn't work, use new Random()
woot thank you!
it should be
var item = yourList[Random.Shared.Next(yourList.Count)];
maxValue parameter is exclusive upper boundWas 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.