-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathRandomNumberGenerator.java
More file actions
33 lines (25 loc) · 1.01 KB
/
RandomNumberGenerator.java
File metadata and controls
33 lines (25 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package model;
import java.util.*;
public class RandomNumberGenerator {
private final Random random;
public RandomNumberGenerator(){
random = new Random();
}
public int pickNumber(int start, int end) {
if (start > end) throw new IllegalArgumentException("숫자의 범위가 올바르지 않습니다.");
return random.nextInt(end-start+1) + start;
}
public List<Integer> pickUniqueNumbers(int start, int end, int count) {
if(end-start+1 < count) throw new IllegalArgumentException("원하는 개수의 숫자를 뽑을 수 없습니다");
if(count<0) throw new IllegalArgumentException("0개 미만의 숫자를 뽑을 수 없습니다.");
List<Integer> list = new ArrayList<>();
Set<Integer> picks = new HashSet<>();
while(list.size() < count){
int pick = pickNumber(start, end);
if(picks.contains(pick)) continue;
list.add(pick);
picks.add(pick);
}
return list;
}
}