본문 바로가기

.NET/C#

메모리에 있는 데이터 랜덤하게 가져오기(Linq)

public static class Extensions
{
	
	public static T NextRandom<T>(this IEnumerable<T> source)
	{
 
		Random gen = new Random((int)DateTime.Now.Ticks);
 
		return source.Skip(gen.Next(0, source.Count() - 1) - 1).Take(1).ToArray()[0];
 
	}
 
	public static IEnumerable<T> Radomized<T>(this IEnumerable<T> source)
	{
 
		List<T> Remaining = new List<T>(source);
 
		while (Remaining.Count >= 1)
		{
 
			T temp = NextRandom<T>(Remaining);
 
			Remaining.Remove(temp);
 
			yield return temp;
 
		}
 
	}
}

부끄러운 얘기지만 가져다 썼다.

참 친절하신 분 많다.ㅋㅋ