서로 다른 랜덤수를 생성합니다
1007 단어 서로 다른 랜덤수
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class Number {
/**
*
* @param count
* @param from
* @param to
* @return count , from to ;
* , count to-from+2
*/
public static Set<Integer> DifRandom(int count, int from, int to) {
Set<Integer> randoms = new HashSet<Integer>();
while (randoms.size() < count) {
int num = (int) (Math.random() * (to - from + 1)) + from;
randoms.add(num);
}
return randoms;
}
public static void main(String[] args) {
long time = new Date().getTime();
Set<Integer> nums = DifRandom(1000000, 10000, 10000000);
Iterator<Integer> it =nums.iterator();
while(it.hasNext()) {
System.out.println(it.next().intValue());
}
System.out.println(" : " + nums.size()+ ", :" + (new Date().getTime() - time) + "ms");
}
}