Java Random String getRandomString()

Here you can find the source of getRandomString()

Description

get Random String

License

Open Source License

Declaration

public static String getRandomString() 

Method Source Code


//package com.java2s;
/*//from   w  w w  .  j  a va2  s  .  com
 *    Team15Alpha
 *    AppName: SkillTradiez (Subject to change)
 *    Copyright (C) 2015  Stephen Andersen, Falon Scheers, Elyse Hill, Noah Weninger, Cole Evans
 *
 *    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/>.
 */

import java.util.Random;

public class Main {
    private static Random random = new Random();
    private static String[] components = new String[] { "" };

    public static String getRandomString() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 5; i++) {
            sb.append(components[random.nextInt(components.length)]);
        }
        sb.append(random.nextInt());
        String result = sb.toString();
        if (result.length() > 9) {
            result = result.substring(0, 9);
        }
        return result;
    }
}

Related

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