Java Random Int randomAlphaNum(int length)

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

Description

Generates random alphanumeric String of the specified length.

License

Apache License

Parameter

Parameter Description
length a parameter

Declaration

public static String randomAlphaNum(int length) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**//  w  w w.ja  va  2  s . co  m
     * Generates random alphanumeric String of the specified length.
     * 
     * @param length
     */
    public static String randomAlphaNum(int length) {
        char[] result = new char[length];
        fillWithRandomAlphaNum(result);
        return new String(result);
    }

    /**
     * Populates given char array with random string. Can be used for memory
     * conservation purposes.
     * 
     * @param str
     */
    public static void fillWithRandomAlphaNum(char[] str) {
        char[] ALPHA_NUM = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray();
        for (int i = 0; i < str.length; i++) {
            str[i] = ALPHA_NUM[(int) (Math.random() * ALPHA_NUM.length)];
        }
    }
}

Related

  1. random(int theRange)
  2. random(int x)
  3. random(int... array)
  4. random_range(int x1, int x2)
  5. randomActorId(int max)
  6. randomAlphanumeric(int count)
  7. randomAlphanumericString(int length)
  8. randomAlphaString(int length)
  9. randomArray(int len)