Friday 15 August 2014

c# - Picking a random element from a Multidimensional Array -


Does anyone know how to select a random element from an array?

I know how to implement at normal variables

  random rnd = new random (); Int no = rnd.Next (30); Console.WriteLine (any);   

But I have to apply it to an array.

This is an example of how to select a random element from an array.

  int [] possible = new int [] {0, 5, 10, 15}; Random R = New Random (); Int a = possible [r. Next (possible length)];   

However, I should keep in mind that if you call it repeatedly, make sure that you only call the last line multiple times. Calling the second line can be repeated repeatedly, because Random () uses the current time as a seed. If the time has not changed, you can get the same result more than once.

On the request of OP: On a two-dimensional array:

  // Suppose an integer [,] random R = new random (); Int a = possible [r. Next (possible. Gate Loughgh (0)), r. Next (possible. Get Lang (1))];    

No comments:

Post a Comment