Here you can find the source of generateRandomString(int length)
length
characters.
Parameter | Description |
---|---|
length | The number of characters the string should have. |
public static String generateRandomString(int length)
//package com.java2s; /*/*w ww . j av a 2 s.c o m*/ * SalSSuite - Suite of programmes for managing a SalS project * Copyright (C) 2011 Jannis Limperg <jannis[dot]limperg[at]arcor[dot]de> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ public class Main { /** * Generates a random string of <code>length</code> characters. The string * consists only of letters and numbers. * @param length The number of characters the string should have. * @return A random string. */ public static String generateRandomString(int length) { String randomString = ""; while (true) { if (randomString.length() >= length) return randomString.substring(0, length - 1); else randomString += java.util.UUID.randomUUID().toString().replaceAll("-", ""); } } }