Java Random String getRandomString(final int len)

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

Description

get Random String

License

Open Source License

Declaration

public static String getRandomString(final int len) 

Method Source Code

//package com.java2s;
/**/*from w  w w  .j  a va 2s.  c  om*/
 * SPINdle (version 2.2.2)
 * Copyright (C) 2009-2012 NICTA Ltd.
 *
 * This file is part of SPINdle project.
 * 
 * SPINdle is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * SPINdle 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 Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with SPINdle.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author H.-P. Lam (oleklam@gmail.com), National ICT Australia - Queensland Research Laboratory 
 */

import java.util.Random;

public class Main {
    private static final String CHARACTORS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    private static Random RANDOM_GENERATOR = new Random(
            System.currentTimeMillis());

    public static String getRandomString(final int len) {
        StringBuilder sb = new StringBuilder();
        int max = CHARACTORS.length();
        for (int i = 0; i < len; i++) {
            sb.append(CHARACTORS.charAt(RANDOM_GENERATOR.nextInt(max)));
        }
        return sb.toString();
    }
}

Related

  1. getRandomString()
  2. getRandomString()
  3. getRandomString()
  4. getRandomString()
  5. getRandomString()
  6. getRandomString(final int len)
  7. getRandomString(final int length)
  8. getRandomString(final int size)
  9. getRandomString(final int size)