Wednesday 25 February 2015

How to generate a random alpha numeric String in java.


package com.digit;

import java.util.Date;
import java.util.Random;

public class RandumString {
public static void main(String args[]){
//Randum ren= new Randum();
String a = Randum.generateRandomString(6);
System.out.println(a);
}
}

class Randum{

private static Random random = new Random((new Date()).getTime());

    public static String generateRandomString(int length) {
      char[] values = {'a','b','c','d','e','f','g','h','i','j',
               'k','l','m','n','o','p','q','r','s','t',
               'u','v','w','x','y','z','0','1','2','3',
               '4','5','6','7','8','9'};

      String out = "";

      for (int i=0;i<length;i++) {
          int idx=random.nextInt(values.length);
        out += values[idx];
      }

      return out;
    }
}


No comments:

Post a Comment