The easiest way is something like this:
var r = new Random(); var myValues = new int[] { 1, 2, 3, 4, 5, 6 };
But the best method, if you want to avoid duplicates, you need to use a shuffle algorithm, such as the Fisher-Yates algorithm, then take the first 3 elements:
public static T[] Shuffle<T>(IEnumerable<T> items) { var result = items.ToArray(); var r = new Random(); for (int i = items.Length; i > 1; i--) { int j = r.Next(i); var t = result[j]; result[j] = result[i - 1]; result[i - 1] = t; } return result; } var myValues = new int[] { 1, 2, 3, 4, 5, 6 };
source share