Home >Backend Development >C++ >How Can I Randomly Select and Display an Element from an ArrayList?
Randomly Accessing and Displaying List Items
This guide demonstrates how to select and display a random string from an ArrayList in response to a button click. Follow these steps:
<code>static Random rnd = new Random();</code>
<code>int randomIndex = rnd.Next(list.Count);</code>
<code>string selectedString = list[randomIndex];</code>
<code>MessageBox.Show(selectedString);</code>
This method ensures a random string from the ArrayList is retrieved and displayed within a message box.
The above is the detailed content of How Can I Randomly Select and Display an Element from an ArrayList?. For more information, please follow other related articles on the PHP Chinese website!