java generating random string

import java.util.*;

public class RandomStr
{
    private final char[] alphanumeric=alphanumeric();
    private final Random rand;

    public RandomStr(){this(null);}

    public RandomStr(Random rand){
      this.rand=(rand!=null) ? rand : new Random();
    }

    public String get(int len){
        StringBuffer out=new StringBuffer();
        
        while(out.length() < len){
            int idx=Math.abs(( rand.nextInt() % alphanumeric.length ));
            out.append(alphanumeric[idx]);
        }
        return out.toString();
    }

    // create alphanumeric char array
    private char[] alphanumeric(){
        StringBuffer buf=new StringBuffer(128);
        for(int i=48; i<= 57;i++)buf.append((char)i); // 0-9
        for(int i=65; i<= 90;i++)buf.append((char)i); // A-Z
        for(int i=97; i<=122;i++)buf.append((char)i); // a-z
        return buf.toString().toCharArray();
    }

    public static void main(String[] args){
        RandomStr rand=new RandomStr();
        System.out.println(
            "10 chars random string="+rand.get(10)
        );
        System.out.println(
            "128 chars random string="+rand.get(128)
        );

    }
}

later on I realize there is one-liner for this

Long.toHexString(Double.doubleToLongBits(Math.random()));
About these ads

6 thoughts on “java generating random string

  1. I don’t understand how does this statement work. Can you show me a small example of how to display the results of this statement. I am very new to Java and have some experience with VBA so I need as much help as possible. I am taking an object oriented programming class this semester but I am way ahead of where the class is. I have been practicing with JAVA for about a month now and the class just started touching it last week. Well I was told one of the best tools for learning is online forums, so here I am. Any help would be great. I am trying to write a Java app and I am looking at different ways to generate unique id’s.

    Thanks
    James

  2. Hello,

    I have tested this program and it looks great but I have a question.
    What is the probability that a number is repeated two times? I mean, I want to know the range time.

    Thanks in advance for your quick response

  3. I’ve not computed probability, it largly depends on probability of Random.nextInt producing numbers in same order. If you are using jdk5 or greater you can use UUID.randomUUID() to generate type 4 UUID, which should produce uniq strings over large sequence.

  4. Hi I’m a java beginner and i was wondering if you could show me a simpler way of generating strings. I’m writing a finite state automaton in Java that checks if a Language is infinite by comparing the length of a string generated by the language to the cardinality of the set of states (Q) of the automaton. any help would be great thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s