특정 값을 제외한 랜덤 한 값 구하기
개발을 진행하다보면 특정한 값을 제외한 랜덤 한 값을 구할 때가 있다.
그런 상황에서 썻던 코드이다.
using System.Linq;
public int GetRandomNotContain(int _min, int _max, int[] _notContainValue)
{
HashSet<int> exclude = new HashSet<int>();
for (int i = 0; i < _notContainValue.Length; i++)
{
exclude.Add(_notContainValue[i]);
}
var range = Enumerable.Range(_min, _max).Where(i => !exclude.Contains(i));
var rand = new System.Random();
int index = rand.Next(_min, _max - exclude.Count);
return range.ElementAt(index);
}
★☆☆☆☆
반응형
댓글