Returns a random String of numbers and letters (lower and upper case) of the specified length. - Android java.util

Android examples for java.util:Random String

Description

Returns a random String of numbers and letters (lower and upper case) of the specified length.

Demo Code

import java.util.Random;

public class Main {
  private static char[] sNumbersAndLetters = ("0123456789abcdefghijklmnopqrstuvwxyz0123456789").toCharArray();


  private static Random sRandGen = new Random();
  public static String randomString(int length) {
    if (length < 1) {
      return null;
    }/*  w w w  . ja va  2 s.c  o  m*/
    // Create a char buffer to put random letters and numbers in.
    final char[] randBuffer = new char[length];
    for (int i = 0; i < randBuffer.length; i++) {
      randBuffer[i] = sNumbersAndLetters[sRandGen.nextInt(sNumbersAndLetters.length - 1)];
    }

    return new String(randBuffer);
  }

}

Related Tutorials