728x90
- 기본개념
Random rnd= new Random();
rnd.nextBoolean(); // return true or false
rnd.nextInt(숫자X); //0~X-1중 하나 리턴
1
2
3
4
5
6
7
8
9
10
11
12
|
//인증키 생성
public String getKey(int key_len) {
Random rnd=new Random();
StringBuffer buf=new StringBuffer();
for(int i=1;i<=key_len;i++) {
if(rnd.nextBoolean())
buf.append((char)(rnd.nextInt(26)+65)); // 0~25(26개) + 65
else
buf.append(rnd.nextInt(10));
}
return buf.toString();
}
|
cs |
4 : 문자열에 계속해서 무언가를 추가할것이기에, 삽입 삭제에 유리한 StringBuffer를 이용한다.
7 : 대문자 알파벳이 나온다.
알파벳은 총 26글자이며 , A의 ASCII는 65이다.
숫자로 아스키값을 먼저 계산후 char 로 형변환하여 문자화 시켜주었다.
'21년이전 > JAVA' 카테고리의 다른 글
JAVA 알고리즘 - String <-> char[] (0) | 2021.06.17 |
---|---|
JAVA - 알고리즘을 위한 클래스,메소드 (0) | 2021.06.15 |