I have an array like this one:
int[] numbers = new [] { 1, 2, 3, 4 };
I'd like to randomize this (different each time) so that it makes another array with the same size and numbers but in a different order each time.
I have an array like this one:
int[] numbers = new [] { 1, 2, 3, 4 };
I'd like to randomize this (different each time) so that it makes another array with the same size and numbers but in a different order each time.
Here's a method that will work:
Edit:
Another way could be using LINQ extension methods for
IEnumerable<T>
collections:If you want to have an array instead of a
List<int>
you can invoke.ToArray()
instead.Of course, that will work for any array of
int
, not only for1, 2, 3, ..., n
. You can even make the method generic inT
.It works:
Try something like this:
This even takes care that the values dont repeat ;
In my eyes the easiest way would be this: