Java Random String getRandomString(final int size)

Here you can find the source of getRandomString(final int size)

Description

Creates a random alpha-numeric String of given length

License

Open Source License

Parameter

Parameter Description
size the length of the returned random String

Return

the random String

Declaration

public static String getRandomString(final int size) 

Method Source Code

//package com.java2s;
/*//from   w  w  w.j ava 2  s.c o  m
 *
 * JMeta - Meta's java implementation
 *
 * Copyright (C) 2013-2015 Pablo Joubert
 * Copyright (C) 2013-2015 Thomas Lavocat
 * Copyright (C) 2013-2015 Nicolas Michon
 *
 * This file is part of JMeta.
 *
 * JMeta is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * JMeta 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

import java.util.Random;

public class Main {
    private static final String AB = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static final Random rnd = new Random();

    /**
     * Creates a random alpha-numeric String of given length
     *
     * @param size the length of the returned random String
     * @return the random String
     */
    public static String getRandomString(final int size) {
        StringBuilder sb = new StringBuilder(size);
        for (int i = 0; i < size; i++) {
            sb.append(AB.charAt(rnd.nextInt(AB.length())));
        }
        return sb.toString();
    }
}

Related

  1. getRandomString()
  2. getRandomString(final int len)
  3. getRandomString(final int len)
  4. getRandomString(final int length)
  5. getRandomString(final int size)
  6. getRandomString(int cantidad, boolean mayusculas, boolean minusculas, boolean numeros, boolean simbolos, boolean repetir)
  7. getRandomString(int count)
  8. getRandomString(int len)
  9. getRandomString(int len)