生成随机数

重复概率概率很小
public class RandomID {
    public static String GenTradeId()    {
        String tradeId = "";
        RandomStrg.setCharset("a-zA-Z0-9");
        RandomStrg.setLength("15");
        try {
            RandomStrg.generateRandomObject();
            tradeId=RandomStrg.getRandom();
        } catch (Exception e){
            System.out.println("e = " + e.toString());
        }
        return tradeId;
    }
    public static String getRandomNum(){
    	
    	Random rd = new Random();	//创建随机对象
		String n="";
		//String num1="";
		int rdGet;	//取得随机数
		do{
		rdGet=Math.abs(rd.nextInt())%10+48;	//产生48到57的随机数(0-9的键位值)
		//rdGet=Math.abs(rd.nextInt())%26+97;	//产生97到122的随机数(a-z的键位值)
		char num1=(char)rdGet;
		String dd=Character.toString(num1);
		n+=dd;

		}while(n.length()<6);//假如长度小于4
		return n;
    }
    
    public static void main(String[] args) {
		System.out.println(getRandomNum());
	}
}

你可能感兴趣的:(java)