Java Random Int randomString(int length)

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

Description

random String

License

Open Source License

Declaration

public final static String randomString(int length) 

Method Source Code

//package com.java2s;
/*//from  w w  w  .  j a v  a 2s  .  co  m
This file is part of Peers, a java SIP softphone.
    
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
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/>.
    
Copyright 2007, 2008, 2009, 2010 Yohann Martineau 
*/

public class Main {
    public final static String randomString(int length) {
        String chars = "abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIFKLMNOPRSTUVWXYZ" + "0123456789";
        StringBuffer buf = new StringBuffer(length);
        for (int i = 0; i < length; ++i) {
            int pos = (int) Math.round(Math.random() * (chars.length() - 1));
            buf.append(chars.charAt(pos));
        }
        return buf.toString();
    }
}

Related

  1. randomStr(int length)
  2. randomStr(int length, String charSet)
  3. randomString(int count)
  4. randomString(int length)
  5. randomString(int length)
  6. randomString(int length)
  7. randomString(int min, int max)
  8. randomString(int numChar)
  9. randomString(int size)