Java Random Int randomString(int numChar)

Here you can find the source of randomString(int numChar)

Description

random String

License

Open Source License

Declaration

public static String randomString(int numChar) 

Method Source Code

//package com.java2s;
/* /*from  www . jav  a 2s. c  om*/
 * Copyright 2012 Devoteam http://www.devoteam.com
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 * 
 * 
 * This file is part of Multi-Protocol Test Suite (MTS).
 * 
 * Multi-Protocol Test Suite (MTS) 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.
 * 
 * Multi-Protocol Test Suite (MTS) 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 Multi-Protocol Test Suite (MTS).
 * If not, see <http://www.gnu.org/licenses/>.
 * 
 */

public class Main {
    public static String randomString(int numChar) {
        StringBuilder s = new StringBuilder();
        for (int j = 0; j < numChar; j++) {
            int nextChar = (int) (Math.random() * 62);
            if (nextChar < 10) //0-9
            {
                s.append(nextChar);
            } else if (nextChar < 36) //a-z
            {
                s.append((char) (nextChar - 10 + 'a'));
            } else //A-Z
            {
                s.append((char) (nextChar - 36 + 'A'));
            }
        }
        return s.toString();
    }
}

Related

  1. randomString(int length)
  2. randomString(int length)
  3. randomString(int length)
  4. randomString(int length)
  5. randomString(int min, int max)
  6. randomString(int size)
  7. randomString(int size, String... keys)
  8. randomStringNumbers(int len)
  9. randomtest(int max)