Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I randomize numbers in an array [duplicate]

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.

like image 366
Dave Heart Avatar asked Nov 30 '22 16:11

Dave Heart


1 Answers

Try something like this:

System.Random rnd = new System.Random();
var numbers = Enumerable.Range(1, 4).OrderBy(r => rnd.Next()).ToArray();
like image 149
Leniel Maccaferri Avatar answered Dec 05 '22 15:12

Leniel Maccaferri