-- JSP, SPRING
Random키 생성
어린왕자악꿍
2011. 12. 8. 12:02
휴대폰 인증번호를 랜덤하게 생성해야 하는 경우가 생겨서 아래와 같이 만들었다.
[Servlet]
public static String getRandomKey(int len)
{
int nSeed = 0;
int nSeedSize = 10;
String strSrc = "0123456789";
String strKey = "";
for(int i=0; i<len; i++)
{
nSeed = (int)(Math.random() * nSeedSize) + 1);
strKey += String.valueOf(strSrc.charAt(nSeed-1));
}
return strKey;
}
[JSP]
getRandomKey(4); // 4자리 랜덤키
만약 영문과 숫자의 조합의 키를 만들기 위해서는 strSrc에 영문을 포함시키고, 자리 수만큼 nSeedSize를 늘려주면 되므로 랜덤키를 만들때 유연한 구조라 생각한다.
[Servlet]
public static String getRandomKey(int len)
{
int nSeed = 0;
int nSeedSize = 10;
String strSrc = "0123456789";
String strKey = "";
for(int i=0; i<len; i++)
{
nSeed = (int)(Math.random() * nSeedSize) + 1);
strKey += String.valueOf(strSrc.charAt(nSeed-1));
}
return strKey;
}
[JSP]
getRandomKey(4); // 4자리 랜덤키
만약 영문과 숫자의 조합의 키를 만들기 위해서는 strSrc에 영문을 포함시키고, 자리 수만큼 nSeedSize를 늘려주면 되므로 랜덤키를 만들때 유연한 구조라 생각한다.