Java Random String generateRandomString(int count)

Here you can find the source of generateRandomString(int count)

Description

Generate a random string.

License

Open Source License

Return

the generated random String

Declaration

public static String generateRandomString(int count) 

Method Source Code

//package com.java2s;
/**/*from  w  w w  .  j a v  a 2s  .  c  o m*/
 * (C) 2011-2012 Alibaba Group Holding Limited.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 */

import java.util.Random;

public class Main {
    /**
     * Generate a random string. This is used for password encryption.
     *
     * @return the generated random String
     */
    public static String generateRandomString(int count) {
        Random random = new Random();
        StringBuffer buffer = new StringBuffer();

        while (count-- != 0) {
            // a random number in the 32...127 range
            char ch = (char) (random.nextInt(96) + 32);
            buffer.append(ch);
        }

        return buffer.toString();
    }
}

Related

  1. generateRandomString()
  2. generateRandomString()
  3. generateRandomString()
  4. generateRandomString(final int minCharLen, final int maxCharLen)
  5. generateRandomString(final String pattern)
  6. generateRandomString(int len)
  7. generateRandomString(int length)
  8. generateRandomString(int length)
  9. generateRandomString(int length)