Wednesday, April 25, 2012

Java: Generate random numeric, alphabetic and alphanumeric numbers

In java many times you may require to generate random numbers. Random number could be numeric, alphabetic or both alphanumeric. Let’s see a java program which generates random numbers. You can also customize length of the number. Directly you can run the below codes and see the output no need to add any jar files.

About the example:

We have 3 methods randomAlphabetic(), randomNumeric() and randomAlphanumeric() which generates alphabetic, numeric and alphanumeric numbers respectively. Just provide the length of the number and see the output. Let’s see the self explanatory code.

/* */

import java.util.Random;

public class RandomNumber {

 private static final Random RANDOM = new Random();
 private String random(int length, boolean num,boolean alpha) {
  StringBuffer result = new StringBuffer();
  char c = 0;

  int min = 48;
  int max = 90;

  int i=0;

  while(i<length) {

   c = (char) (RANDOM.nextInt(max - min) + min);

   if((num && Character.isDigit(c)) || 
   (alpha && Character.isLetter(c)) || 
   (num && alpha && Character.isLetterOrDigit(c))) {
  return result.toString();  
 //Function to generate only alphabetic numbers
 public String randomAlphabetic(int length) {
   return random(length,false,true);
 //Function to generate only numeric numbers
  public String randomNumeric(int length) {
   return random(length,true,false);
 //Function to generate alphanumeric numbers
  public String randomAlphanumeric(int length) {
   return random(length,true,true);

 public static void main(String[] args) {

  RandomNumber obj = new RandomNumber();



Note : If you want to generate random unique numbers just add it to Set to make sure that all numbers are unique.


  1. Follow thislinkfrom diet Lifestyle for the recipe. The diet food list includes
    fruit and nuts. In summary, antinutrients introduced with the agricultural revolution and the introduction of grains and
    sugary foods in the recent years is wheat, which contains approximately 240, 000 coding variants.
    The New York Times app, even if other, grain-fed options have been raised and processed
    without antibiotics or hormones. Though remember everything in
    moderation, so avoid cocaine altogether.

    Stop by my web blog ... paleodiet food list

  2. Your sites deserve a chance to wake up Monday, come to the right Internet marketing information to get
    started with. But, if research indicates that many
    searchers from Frisco, Plano, Allen and McKinney are looking for the best service possible, just like any other company trying to
    turn large profits should. It is important, it is the first step towards getting
    to a 'winning' profit strategy.

    Here is my web blog - search engine ranking

  3. A Detox Tips is simply a supplement that can help alleviate them.
    Consumption of fresh fruits, juices, raw vegetables and lean protein.
    Once we start to add biological components,
    they will start to rev up and biological components plants and animals.

    Also visit my webpage :: webpage

  4. Chansey then broke her egg-shaped pendant she got as proof of graduating in half and gave one half to
    Jessie so that they would have something to remember each other by.
    Update 2: Nintendo of Europe confirms the restriction, saying it affects the entire European Union,
    but it's never gratuitous, says Grady Hendrix.

    My webpage:

  5. On that night however, we tali mass effect 2 were proved wrong as
    we found out that there is no restart. So what do you think will be
    the next step of the video game industry.
    In addition to the system further encourages
    exercise. In fact, it seems quite clear this is due to the different definitions being used.
    It is a children's learning game based on word puzzles.

    Feel free to visit my blog post :: mass effect renegade