난수를 발생할 때, 중복되지 않는 것들로만 해야할 때가 있다.
이와 같은 경우에 사용할 수 있는 예제를 하나 만들어 보았다.
List Generic Collection을 사용해서 만들었다.
이와 같은 경우에 사용할 수 있는 예제를 하나 만들어 보았다.
List Generic Collection을 사용해서 만들었다.
using System;
using System.Collections.Generic;
namespace RandomNumber
{
class Program
{
static void Main(string[] args)
{
Random myRandom = new Random();
List<int> myList = new List<int>();
int r;
for (int i = 0; i < 3; i++)
{
do
{
r = myRandom.Next(5);
} while (myList.Contains(r));
myList.Add(r);
}
foreach (int item in myList)
{
Console.WriteLine(item);
}
}
}
}
'Code > C#' 카테고리의 다른 글
| [C#] Thread 샘플 코드 1 (0) | 2007.12.07 |
|---|---|
| [C#] Extension Methods 샘플 코드 (0) | 2007.12.02 |
| [C#] Jagged Array 선언 방법 (0) | 2007.11.29 |
| [C#] 3차원 Array 샘플 코드 (0) | 2007.11.29 |
| [C#] Multidimensional Array 선언하는 방법 (0) | 2007.11.29 |