隨機生成四個驗證碼

weixin_33858249發表於2017-11-07
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class ArrayRandomData {
    char[] datas ;
    public ArrayRandomData() {
        String str = "abcdefghijklmnopqrstuvwxyz1234567890";
        datas = str.toCharArray();
    }
    public String getRandomData() {
        char[] rands = new char[4];
        int index;
        for(int i=0;i<4;i++) {
            index = (int)(Math.random()*datas.length);
            rands[i] = datas[index];
        }
        return new String(rands);
    }
    public static void main(String[] args) {
        ArrayRandomData ard = new ArrayRandomData();
        String randomData = ard.getRandomData();
        System.out.println("隨機數是:"+randomData);
    }
}


      本文轉自arac 51CTO部落格,原文連結:http://blog.51cto.com/skyarac/1351012,如需轉載請自行聯絡原作者




相關文章