1. 程式人生 > >java獲取隨機密碼

java獲取隨機密碼

import java.util.Random;


public class tests {

    /**
     * 
     * author LiuQiang
     * date 2013-10-14 下午01:13:54
     * @param args
     */
    public static void main(String[] args) {
        char[] ss = new char[10];
         int[] flag = {0,0,0}; //A-Z, a-z, 0-9
         int i=0;
        while(flag[0]==0 || flag[1]==0 || flag[2]==0 || i<10) {
            i 
= i%10; int f = (int) (Math.random()*3%3); if(f==0) ss[i] = (char) ('A'+Math.random()*26); else if(f==1) ss[i] = (char) ('a'+Math.random()*26); else ss[i] = (char) ('0'+Math.random()*10); flag[f]
=1; i++; } Random rd = new Random(); char[] a = {'@','#','$','%','&','*'}; int max = ss.length; int min = 1; for(int n=0;n<2;n++){ int indexa = rd.nextInt(5); int indexss = rd.nextInt(max)%(max-min+1) + min; ss[indexss]
=a[indexa]; } System.out.println(new String(ss)); } }