The FromArray
method is an extension method for the System.Random
class. It allows you to randomly select an element from a given array. If the array is empty or null, it returns a default value specified by the caller.
The FromArray
method is an extension method for the System.Random
class. It allows you to randomly select an element from a given array. If the array is empty or null, it returns a default value specified by the caller.
To use the FromArray
method, you need to have an instance of System.Random
. You can then call this method on the instance, passing the array from which you want to select a random element and the default value to return if the array is empty or null.
// Example usage of FromArray method Random random = new Random(); int[] numbers = { 1, 2, 3, 4, 5 }; int defaultValue = -1; int randomElement = random.FromArray(numbers, defaultValue); // randomElement will be a random number from the array or -1 if the array is empty or null.