CSharp · Console
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
private static readonly Random _r = new Random();
private static IEnumerable<(T, T)> FunkyLotto<T>(IEnumerable<T> source)
{
var pool = source.ToList();
var iteration = pool.ToList();
foreach (var item in iteration)
{
int choice;
if (pool.Count == 2 && pool.IndexOf(item) >= 0) // special case when there is only 2 left, and there is a case for a duplicate
choice = (pool.IndexOf(item) + 1) % pool.Count;
else
choice = _r.Next(0, pool.Count);
choice = Equals(item, pool[choice]) ? (choice + 1) % pool.Count : choice;
yield return (item, pool[choice]);
pool.RemoveAt(choice);
}
}
public static void Main()
{
var list = new[]{"Joe", "John", "Chris", "Henry", "Tom", "Patrick"};
foreach (var result in FunkyLotto(list))
Console.WriteLine(result);
}
}