how to generate a random string

Discussion in 'Java' started by Jacob, Feb 22, 2006.

  1. Jacob

    Jacob Guest

    As an aid for unit testing I want to write a method
    that generate a random string of a specified length:

    String getRandom(int length)
    {
    // magic
    }

    My current approach is to generate n random bytes
    and then convert them to a string using the String
    ctor.

    This *do* produce a string, but there are comments in
    the String docs that worries me.

    Is there more to this?

    Thanks!
     
    Jacob, Feb 22, 2006
    #1
    1. Advertisements

  2. I would recommend the approach to generate n random *chars* (may be
    restricted to the printable range from '\u0020' to '\u007E') and to make
    a string from it using the String(char[]) constructor.
    To which of the many constructors
    String(byte[], ...)
    do you refer here?
    Which comment? (I'm not a mind-reader)
     
    Thomas Fritsch, Feb 22, 2006
    #2
    1. Advertisements

  3. Jacob

    Jacob Guest

    And how do I make a random char?

    Being "printable" is not an issue; I expect the most spectacular
    errors are uncovered by the non-printable ones... :)
    String(byte[] bytes)
    The paragraph about the CharsetDecoder. jdk 1.5.0_06
     
    Jacob, Feb 22, 2006
    #3
  4. Jacob

    Oliver Wong Guest

    You could generate an int within the valid range of char, and then do a
    cast.

    <code>
    Random RNG = new Random();
    char = (char)(RNG.nextInt(Character.MAX_VALUE + 1));
    </code>

    the +1 is because the upper bound is considered to be exclusive instead of
    inclusive.

    - Oliver
     
    Oliver Wong, Feb 22, 2006
    #4
  5. Jacob

    Chris Uppal Guest

    Generating random Strings by generating random bytes will get you into trouble.
    Not all sequences of bytes correspond to legal encodings of character data
    (depending on what character encoding you use). Similarly not all sequences of
    N bytes will decode to the same length String.

    Why not generate random chars ? They are numbers too, so you can generate them
    just as easily as bytes. Use a StringBuilder (or StringBuffer) to assemble
    them into a String.

    Generating strings of /readable/ characters takes a little more work, but not
    much.

    -- chris
     
    Chris Uppal, Feb 22, 2006
    #5
  6. Jacob

    robert Guest

    Jacob escreveu:
    Use java.util.UUID:

    String getRandom(int length) {
    // magic
    UUID uuid = UUID.randomUUID();
    String myRandom = uuid.toString()
    return myRandom.substring(length);
    }

    A UUID represents a 128-bit value. If you need more than that I suppose
    just concat uuid's together.

    HTH,
    iksrazal
    http://www.braziloutsource.com/
     
    robert, Feb 22, 2006
    #6
  7. For generating random numbers just use a random generator which you then
    cast to a char and add to a string. See Olivers example. You can for
    example use a char array and add the array to a stringBuffer in a loop.

    If you want random printable characters, then you create an array of all
    legal characters starting form 0..N and take the random number modulo
    the array length, that should give you a random printable character from
    the array.

    When it comes to whether the string needs or do not need to be
    printable, I think you should think it through. Non printable characters
    in a string can cause havoc if you are not prepared for it.

    /tom
     
    tom fredriksen, Feb 22, 2006
    #7
  8. Jacob

    Stefan Ram Guest

    To find such a method, I look up my catalogue in the category

    :catalogue:Treeg:programming:language/Java:libraries:string processing
    http://purl.net/stefan_ram/garnoo/X...ANGUAGEXSYJAVAXOLIBRARIESXOSTRINGXGPROCESSING

    The relevant (and only) entry is

    http://nd-com.net/downloads/RandomString.java

    that shows how to get a random string matching a given regular
    expression.
     
    Stefan Ram, Feb 22, 2006
    #8
  9. Jacob

    steve Guest


    how long do they have to be?

    short strings < 100 chars or biggy stuff 32k and the likes.

    steve
     
    steve, Feb 23, 2006
    #9
  10. Jacob

    Roedy Green Guest

    Roedy Green, Feb 24, 2006
    #10
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.