Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

9
  • 74
    +1, the simplest solution here for generating a random string of specified length (apart from using RandomStringUtils from Commons Lang). Commented Apr 20, 2012 at 15:49
  • 15
    Consider using SecureRandom instead of the Random class. If passwords are generated on a server, it might be vulnerable to timing attacks. Commented Jun 25, 2014 at 13:34
  • 10
    I would add lowercase also: AB = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; and some other allowed chars. Commented Sep 7, 2015 at 20:56
  • 1
    Why not put static Random rnd = new Random(); inside the method? Commented Feb 8, 2016 at 1:25
  • 6
    @MicroR Is there a good reason to create the Random object in each method invocation? I don't think so. Commented Feb 15, 2016 at 10:49