5

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!

HansRüdi
  • 63
  • 6
  • https://stackoverflow.com/questions/273313/randomize-a-listt – jspcal May 28 '18 at 05:54
  • @jspcal items could appear multiple times here `[270, 180, 180, 90, ...]` e.g. `180` – fubo May 28 '18 at 05:55
  • Check this: https://stackoverflow.com/questions/17530306/getting-random-numbers-from-a-list-of-integers – PM. May 28 '18 at 05:55
  • Pick through random index and remove from the source list – Icepickle May 28 '18 at 05:58
  • `var rnd = new Random(); randomRotationVoidList = randomRotationVoidList.OrderBy(x => rnd.Next()).ToList()` - it's best to define `rnd` as a class-level field though to avoid issues with possible duplicated values. – Enigmativity May 28 '18 at 05:58
  • Possible duplicate of [Randomize a List](https://stackoverflow.com/questions/273313/randomize-a-listt) – FoggyFinder May 28 '18 at 11:41

1 Answers1

9

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();
fubo
  • 44,811
  • 17
  • 103
  • 137
  • I would call the second approach "more generic". – Enigmativity May 28 '18 at 06:17
  • 3
    @Enigmativity nah.. it's less generic in the sense that it is hardcoded and computes. result will be `int` whereas first one can result in whatever type is in array. – Brett Caswell May 28 '18 at 06:19
  • My view is that it doesn't use a hard-coded array. It's more generic in that it uses a formula rather than a specific array. – Enigmativity May 28 '18 at 06:23
  • @Enigmativity but you could replace that array with any type of array of a varaible length e.g. `public static IEnumerable GetRandomList(T[] dictinary, int entries)` so it could be reused for other purposes - and that's the definition of _generic_ – fubo May 28 '18 at 06:28
  • well.. it isn't going to be out of bounds, if that is a worry.. but think of the same sample code but with a different array type: `string[] input = { "90", "180", "270", "360" }` .. we mean `Type` generic .. – Brett Caswell May 28 '18 at 06:28
  • 1
    Likewise `public static IEnumerable GetRandomList(Func dictinary, int entries)` – Enigmativity May 28 '18 at 06:33
  • @Enigmativity I don't really want to make a discussion board out of this, but if you're going to pass `Func` into `Select` you cannot guarantee a random behavior, since that would have to be defined in the delegate you're passing. Anyhow, it's good answer. – Brett Caswell May 28 '18 at 06:38