Java examples for java.util:Random String
get Random Nonce by length
//package com.java2s; import java.util.Random; public class Main { public static void main(String[] argv) throws Exception { int length = 2; System.out.println(getRandomNonce(length)); }//from ww w.j av a 2 s. co m public static String getRandomNonce(int length) { StringBuffer buffer = new StringBuffer( "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"); StringBuffer sb = new StringBuffer(); Random r = new Random(); int range = buffer.length(); for (int i = 0; i < length; i++) { sb.append(buffer.charAt(r.nextInt(range))); } return sb.toString(); } }