Java UUID Create generateRandomString(int length)

Here you can find the source of generateRandomString(int length)

Description

Generates a random string of length characters.

License

Open Source License

Parameter

Parameter Description
length The number of characters the string should have.

Return

A random string.

Declaration

public static String generateRandomString(int length) 

Method Source Code

//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("-", "");
        }
    }
}

Related

  1. generateID()
  2. generateId()
  3. generateRandomMap(int count)
  4. generateRandomName()
  5. GenerateRandomSlug()
  6. generateRandomTableName()
  7. generateRandomUUID()
  8. generateRandomUUID(final int length)
  9. generateRawUUID()