Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,390 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I am working on a character selector and I want it so the user to have the ability to randomly select and character from a click of a button.
Thank you soo much ;)
Use Random to randomly select a value. Suppose you have 3 characters to choose from. Then you'd do something like this.
//Do this once at startup of your app or when creating the instance that handles the random logic
var random = new Random();
//Select 1 of 3
var selectedValue = random.Next(1, 4);
This would give all 3 equal chances of selection. Just wrap that code in the Click
handler of the button (or in a helper type depending upon your app needs).
private void OnSelectCharacter ( object sender, EventArgs e )
{
//Assume the characters are stored in an array called _characters
//Select a random index of the available characters
var selectedIndex = random.Next(0, _characters.Length);
//Get the selected character
var selectedCharacter = _characters[selectedIndex];
}