generate a random array in csharp

To generate a random array in C#, you can use the Random class and a loop to initialize the elements with random values. Here's an example:

main.cs
int[] array = new int[10]; // create an array with 10 elements
Random random = new Random(); // create a Random object

for (int i = 0; i < array.Length; i++)
{
    array[i] = random.Next(100); // set element i to a random number between 0 and 99
}
249 chars
8 lines

In this example, we create an array with 10 elements and a new instance of the Random class. We then loop through each element of the array and set it to a random integer between 0 and 99 using the Next() method of the Random object.

You can adjust the size of the array and the range of the random numbers by changing the arguments to the respective constructors or methods.

gistlibby LogSnag