shuffle lotto

CSharp · Console

/Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
					
public class Program
{
	public static void Main()
	{
		var lotto = Enumerable.Range(0, 50).Shuffle(new Random()).Take(7);
		Console.WriteLine("the new lotto winning numbers are: {0}", string.Join(",", lotto));
	}

}

public static class Extensions
{
	public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source, Random rng)
    {
        T[] elements = source.ToArray();
        for (int i = elements.Length - 1; i >= 0; i--)
        {
            int swapIndex = rng.Next(i + 1);
            yield return elements[swapIndex];
            elements[swapIndex] = elements[i];
        }
    }
}
;