Oracle2009. 3. 11. 10:56
SELECT DBMS_RANDOM.RANDOM FROM DUAL; --랜덤 숫자 (양수/음수) 

SELECT DBMS_RANDOM.VALUE FROM DUAL; --랜덤 숫자 (0~1 사이) 

SELECT DBMS_RANDOM.VALUE(1,1000) RANDOM FROM DUAL;  --랜덤 숫자 (1~1000 사이)

SELECT DBMS_RANDOM.VALUE(100000000000, 999999999999) RANDOM FROM DUAL; --랜덤 숫자 (천억~조-1 사이) 

SELECT DBMS_RANDOM.STRING('U', 20) STR FROM DUAL; --대문자 20글자 랜덤

SELECT DBMS_RANDOM.STRING('L', 20) STR FROM DUAL; --소문자 20글자 랜덤 

SELECT DBMS_RANDOM.STRING('A', 20) STR FROM DUAL; --대문자+소문자 20글자 랜덤

SELECT DBMS_RANDOM.STRING('X', 20) STR FROM DUAL; --대문자+숫자 20글자 랜덤

SELECT DBMS_RANDOM.STRING('P', 20) STR FROM DUAL; --대문자+소문자+숫자+특수문자 20글자 랜덤 


참고 : http://blog.naver.com/bullian/9061415

Posted by Main()
Java2009. 3. 10. 14:51

class RandomString {
    private static String pattern = "[a-zA-Z0-9]+$";
    private static StringBuffer returnString = null;

    public static void main(String[] args) {
        RandomString rndStr = new RandomString();

        System.out.println("[랜덤 문자열]");
        System.out.println("숫자 (50자리) : " + rndStr.getString(50,"1"));
        System.out.println("대문자 (16자리) : " + rndStr.getString(16,"A"));
        System.out.println("소문자 (16자리) : " + rndStr.getString(16,"a"));
        System.out.println("대문자 + 숫자 (32자리) : " + rndStr.getString(32,"A1"));
        System.out.println("소문자 + 숫자 (32자리) : " + rndStr.getString(32,"a1"));
        System.out.println("대문자 + 소문자 + 숫자 (32자리) : " + rndStr.getString(32,""));
    }

    //Overload Constructor
    public RandomString() { returnString = new StringBuffer(); }
    public RandomString(int len) { returnString = new StringBuffer(); this.setLength(len); }
    public RandomString(String type) { returnString = new StringBuffer(); this.setType(type); }
    public RandomString(int len, String type) { returnString = new StringBuffer(); this.setLength(len); this.setType(type); }
    public RandomString(String type, int len) { returnString = new StringBuffer(); this.setType(type); this.setLength(len); }

    //Get Random Character
    private static void getRndChar() {
        int r = (int)Math.round(Math.random() * 1000);

        java.util.Random random = new java.util.Random();
        int rnd = random.nextInt(1000);

        if(test(pattern, String.valueOf((char)rnd))) {
            returnString.append((char)rnd);
        } else {
            getRndChar();
        }
    }

    //Regular Express Check Function
    private static boolean test(String pattern, String value) {
        java.util.regex.Pattern p = java.util.regex.Pattern.compile(pattern);
        java.util.regex.Matcher m = p.matcher(value);
        boolean r = m.matches();

        return r;
    }

    //Set String Length
    public void setLength(int len) {
        returnString.setLength(0);
        for(int i=0; i<Math.abs(len); i++) {
            getRndChar();
        }
    }

    //Set String Pattern Type
    public void setType(String type) {
        if(type.equals("1")) pattern = "[0-9]+$";
        else if(type.equals("A")) pattern = "[A-Z]+$";
        else if(type.equals("a")) pattern = "[a-z]+$";
        else if(type.equals("A1")) pattern = "[A-Z0-9]+$";
        else if(type.equals("a1")) pattern = "[a-z0-9]+$";
        else pattern = "[a-zA-Z0-9]+$";
    }

    //Get Random String
    public String getString() {
        return returnString.toString();
    }

    //Overload getString()
    public String getString(int len, String type) {
        this.setType(type);
        this.setLength(len);
        return this.getString();
    }
}

'Java' 카테고리의 다른 글

로또번호 랜덤생성 자바소스  (0) 2009.03.17
Base64 encode & decode in Java  (0) 2009.03.17
JSTL forEach forToken  (0) 2008.10.02
스타시디키 checksum Java & C# 소스  (0) 2008.09.26
Posted by Main()
Javascript2009. 3. 4. 16:51

가끔 의미없는 랜덤문자열이 필요할때가 있는데 Timestamp 같은 날짜가 관련된것 보다는
정말 아무런 의미도 없는 무작위 문자열을 필요한 길이 만큼 생성해서 사용할 수 있도록 구현해 보았다.


function RndStr() {

    this.str = '';
    this.pattern = /^[a-zA-Z0-9]+$/;

    this.setStr = function(n) {
        if(!/^[0-9]+$/.test(n)) n = 0x10;
        this.str = '';
        for(var i=0; i<n-1; i++) {
            this.rndchar();
        }
    }

    this.setType = function(s) {
        switch(s) {
            case '1' : this.pattern = /^[0-9]+$/; break;
            case 'A' : this.pattern = /^[A-Z]+$/; break;
            case 'a' : this.pattern = /^[a-z]+$/; break;
            case 'A1' : this.pattern = /^[A-Z0-9]+$/; break;
            case 'a1' : this.pattern = /^[a-z0-9]+$/; break;
            default : this.pattern = /^[a-zA-Z0-9]+$/;
        }
    }

    this.getStr = function() {
        return this.str;
    }

    this.rndchar = function() {
        var rnd = Math.round(Math.random() * 1000);

        if(!this.pattern.test(String.fromCharCode(rnd))) {
            this.rndchar();
        } else {
            this.str += String.fromCharCode(rnd);
        }
    }
}

var rndstr = new RndStr();

rndstr.setType(0); //대문자+소문자+숫자
rndstr.setStr(32); //32자리
document.write(rndstr.getStr() + "<br />");

rndstr.setType('a'); //소문자
rndstr.setStr(16); //16자리
document.write(rndstr.getStr() + "<br />");

rndstr.setType('A'); //대문자
rndstr.setStr(16); //16자리
document.write(rndstr.getStr() + "<br />");

rndstr.setType('1'); //숫자
rndstr.setStr(10); //10자리
document.write(rndstr.getStr() + "<br />");

'Javascript' 카테고리의 다른 글

sleep() in JavaScript  (0) 2009.07.15
자바스크립트 숫자 세자리마다 콤마(,) 자동 찍기  (0) 2009.03.31
자바스크립트 폼 리셋  (0) 2009.03.31
Posted by Main()