Here you can find the source of generateRandomStr(int length)
Parameter | Description |
---|---|
length | Length of string |
public static String generateRandomStr(int length)
//package com.java2s; //License from project: Open Source License import java.util.Random; public class Main { /**//from w w w. j ava 2 s . co m * Generate a random string. * @param length Length of string * @return Random string of given length */ public static String generateRandomStr(int length) { String candidateChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; StringBuilder sb = new StringBuilder(); Random random = new Random(); for (int i = 0; i < length; i++) { sb.append(candidateChars.charAt(random.nextInt(candidateChars.length()))); } return sb.toString(); } }