11

If I have a list of integers:

List<int> myValues = new List<int>(new int[] { 1, 2, 3, 4, 5, 6 } );

How would I get 3 random integers from that list?

Zbigniew
  • 27,184
  • 6
  • 59
  • 66
George Harnwell
  • 784
  • 2
  • 9
  • 19

7 Answers7

23

One simple way:

Random r = new Random();
IEnumerable<int> threeRandom = myValues.OrderBy(x => r.Next()).Take(3);

The better way: Fisher–Yates shuffle:

public static class EnumerableExtensions
{
    public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source)
    {
        return source.Shuffle(new Random());
    }

    public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source, Random rng)
    {
        if (source == null) throw new ArgumentNullException("source");
        if (rng == null) throw new ArgumentNullException("rng");

        return source.ShuffleIterator(rng);
    }

    private static IEnumerable<T> ShuffleIterator<T>(
        this IEnumerable<T> source, Random rng)
    {
        List<T> buffer = source.ToList();
        for (int i = 0; i < buffer.Count; i++)
        {
            int j = rng.Next(i, buffer.Count);
            yield return buffer[j];

            buffer[j] = buffer[i];
        }
    }
}

how you use it:

IEnumerable<int> threeRandom = myValues.Shuffle().Take(3);
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
5

The simplest way would be something like this:

var r = new Random();
var myValues = new int[] { 1, 2, 3, 4, 5, 6 }; // Will work with array or list
var randomValues = Enumerable.Range(0, 3)
    .Select(e => myValues[r.Next(myValues.Length)]);

But a better method, if you want to ensure there are no duplicates is to use a shuffling algorithm, like the Fisher-Yates algorithm, then take the first 3 items:

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 }; // Will work with any enumerable
var randomValues = myValues.Shuffle().Take(3);
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
  • But since permutations are randomly generated, they will repeat . What if I want to get all the permutations for a n digit number and save it somewhere. ? – Avan Mar 05 '18 at 03:29
3

or this:

myList.OrderBy(x => Guid.newGuid()).Take(3)
Peter Kellner
  • 14,748
  • 25
  • 102
  • 188
2

Combining the other answer with this answer can lead you to the following:

var rand = new Random();
var numbers = Enumerable.Range(1, 6).OrderBy(i => rand.Next()).ToList();

In this case 1 is the starting value (inclusive) and 6 is the number of integers to generate.

Community
  • 1
  • 1
Jim Buck
  • 2,383
  • 23
  • 42
0

There are ways of doing it! A simple google can fetch you hundreds of answers. However, you can do this!

myList.OrderBy(x => rnd.Next()).Take(3)
CodeMad
  • 950
  • 2
  • 12
  • 30
0

Use the below code to get the number:

int k = 3; // items to select
var items = new List<int>(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 });
var selected = new List<int>();
var neededItem = k;
var availableItem = items.Count;
var rand = new Random();
while (selected.Count < k) {
   if( rand.NextDouble() < neededItem / availableItem ) {
      selected.Add(items[availableItem-1])
      neededItem--;
   }
   availableItem--;
}
Jack
  • 253
  • 3
  • 8
  • 1
    This can select the same item more than once. Also, there's no need to get a random double and manipulate it, just use `Next` to get a random int between two numbers. – Servy Jul 08 '13 at 15:28
0
int[] x = {1,2,3,4};

string result =  Convert.ToString(x[(new Random()).Next(4)]);
rnrneverdies
  • 15,243
  • 9
  • 65
  • 95