Java Random String generateRandomString(int length)

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

Description

generate Random String

License

Open Source License

Declaration

public static String generateRandomString(int length) 

Method Source Code

//package com.java2s;
/*//from  w w  w .ja va2 s  .co  m
 * Copyright (C) 2012 Viettel Telecom. All rights reserved.
 * VIETTEL PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

import java.util.Random;

public class Main {
    private static String RANDOM_STRING = "0123456789 _ aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ";

    public static String generateRandomString(int length) {
        Random rngRandom = new Random();
        char[] text = new char[length];
        int lengRnd = RANDOM_STRING.length();
        for (int i = 0; i < length; i++) {
            text[i] = RANDOM_STRING.charAt(rngRandom.nextInt(lengRnd));
        }
        return new String(text);
    }
}

Related

  1. generateRandomString(int length)
  2. generateRandomString(int length)
  3. generateRandomString(int length)
  4. generateRandomString(int length)
  5. generateRandomString(int length)
  6. generateRandomString(int Length, String caseType)
  7. generateRandomString(int length, String charset)
  8. generateRandomString(int minLength, int maxLength, int minLCaseCount, int minUCaseCount, int minNumCount, int minSpecialCount)
  9. generateRandomString(int n)