Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Randomly fill list with four different numbers

Tags:

c#

random

I've got a list (randomRotationVoidList) that needs to be filled with four different numbers (90, 180, 270, 360) in a random order, e. g. [270, 180, 180, 90, ...]. All I've found so far will generate a list with random numbers between a certain range.

Thanks in advance!

like image 478
HansRüdi Avatar asked May 28 '18 05:05

HansRüdi


1 Answers

Sample for 200 numbers

Random _rnd = new Random();
int[] input = { 90, 180, 270, 360 }; // dictionary of available numbers
List<int> result = Enumerable.Range(0, 200).Select(x => input[_rnd.Next(0, input.Length)]).ToList();

Another approach if the number pattern is fixed to x * 90

Random _rnd = new Random();
List<int> result = Enumerable.Range(0, 200).Select(x => 90 * _rnd.Next(1, 5)).ToList();
like image 125
fubo Avatar answered Oct 15 '22 01:10

fubo