Answer by SoBeRich for How to generate a random alpha-numeric string?
Efficent and short. /** * Utility class for generating random Strings. */ public interface RandomUtil { int DEF_COUNT = 20; Random RANDOM = new SecureRandom(); /** * Generate a password. * * @return...
View ArticleAnswer by mike for How to generate a random alpha-numeric string?
Here is a Java 8 solution based on streams. public String generateString(String alphabet, int length) { return generateString(alphabet, length, new SecureRandom()::nextInt); } // nextInt = bound ->...
View ArticleAnswer by FileInputStream for How to generate a random alpha-numeric string?
I think this is the smallest solution here, or nearly one of the smallest: public String generateRandomString(int length) { String randomString = ""; final char[] chars =...
View ArticleAnswer by Prasad Parab for How to generate a random alpha-numeric string?
public static String getRandomString(int length) { char[] chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRST".toCharArray(); StringBuilder sb = new StringBuilder(); Random random = new Random();...
View ArticleAnswer by aaronvargas for How to generate a random alpha-numeric string?
Here's a simple one-liner using UUIDs as the character base and being able to specify (almost) any length. (Yes, I know that using a UUID has been suggested before) public static String randString(int...
View ArticleAnswer by Patrik Bego for How to generate a random alpha-numeric string?
Don't really like any of this answers regarding "simple" solution :S I would go for a simple ;), pure java, one liner (entropy is based on random string length and the given character set): public...
View ArticleAnswer by kyxap for How to generate a random alpha-numeric string?
Also you can generate any lower or UPPER case Letters or even special chars thought data from ASCII table. For example, generate upper case letters from A (DEC 65) to Z (DEC 90): String...
View ArticleAnswer by patrickf for How to generate a random alpha-numeric string?
This is easily achievable without any external libraries. 1. Cryptographic Pseudo Random Data Generation First you need a cryptographic PRNG. Java has SecureRandom for that typically uses the best...
View ArticleAnswer by user_3380739 for How to generate a random alpha-numeric string?
Here is the one line code by AbacusUtil String.valueOf(CharStream.random('0', 'z').filter(c -> N.isLetterOrDigit(c)).limit(12).toArray()) Random doesn't mean it must be unique. to get unique...
View ArticleAnswer by user5138430 for How to generate a random alpha-numeric string?
Maybe this is helpful package password.generater; import java.util.Random; /** * * @author dell */ public class PasswordGenerater { /** * @param args the command line arguments */ public static void...
View ArticleAnswer by Kristian Kraljic for How to generate a random alpha-numeric string?
Using UUIDs is insecure, because parts of the UUID arn't random at all. The procedure of @erickson is very neat, but does not create strings of the same length. The following snippet should be...
View ArticleAnswer by Steven L for How to generate a random alpha-numeric string?
Yet another solution.. public static String generatePassword(int passwordLength) { int asciiFirst = 33; int asciiLast = 126; Integer[] exceptions = { 34, 39, 96 }; List<Integer> exceptionsList =...
View ArticleAnswer by Howard Lovatt for How to generate a random alpha-numeric string?
An alternative in Java 8 is: static final Random random = new Random(); // Or SecureRandom static final int startChar = (int) '!'; static final int endChar = (int) '~'; static String randomString(final...
View ArticleAnswer by deepakmodak for How to generate a random alpha-numeric string?
Change String characters as per as your requirements. String is immutable. Here StringBuilder.append is more efficient than string concatenation. public static String getRandomString(int length) {...
View ArticleAnswer by neuhaus for How to generate a random alpha-numeric string?
You can use the UUID class with its getLeastSignificantBits() message to get 64bit of Random data, then convert it to a radix 36 number (i.e. a string consisting of 0-9,A-Z): Long.toString(Math.abs(...
View ArticleAnswer by Burak T for How to generate a random alpha-numeric string?
You can create a character array which includes all the letters and numbers, then you can randomly select from this char array and create your own string password. char[] chars = new char[62]; // sum...
View ArticleAnswer by duggu for How to generate a random alpha-numeric string?
public static String randomSeriesForThreeCharacter() { Random r = new Random(); String value=""; char random_Char ; for(int i=0; i<10;i++) { random_Char = (char) (48 + r.nextInt(74));...
View ArticleAnswer by Jamie for How to generate a random alpha-numeric string?
Lots of use of StringBuilder above. I guess it's easy, but requires a function call per char, growing an array, etc... If using the stringbuilder, a suggestion is to specify the required capacity of...
View ArticleAnswer by Vin Ferothas for How to generate a random alpha-numeric string?
public static String getRandomString(int length) { String randomStr = UUID.randomUUID().toString(); while(randomStr.length() < length) { randomStr += UUID.randomUUID().toString(); } return...
View ArticleAnswer by Prasobh.Kollattu for How to generate a random alpha-numeric string?
You can use following code , if your password mandatory contains numbers alphabetic special characters: private static final String NUMBERS = "0123456789"; private static final String UPPER_ALPHABETS =...
View ArticleAnswer by Michael Allen for How to generate a random alpha-numeric string?
Surprising no-one here has suggested it but: import java.util.UUID UUID.randomUUID().toString(); Easy. Benefit of this is UUIDs are nice and long and guaranteed to be almost impossible to collide....
View ArticleAnswer by hridayesh for How to generate a random alpha-numeric string?
using apache library it can be done in one line import org.apache.commons.lang.RandomStringUtils; RandomStringUtils.randomAlphanumeric(64); here is doc...
View ArticleAnswer by rina for How to generate a random alpha-numeric string?
public static String generateSessionKey(int length){ String alphabet = new String("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"); //9 int n = alphabet.length(); //10 String result =...
View ArticleAnswer by Ugo Matrangolo for How to generate a random alpha-numeric string?
Here it is a Scala solution: (for (i <- 0 until rnd.nextInt(64)) yield { ('0' + rnd.nextInt(64)).asInstanceOf[Char] }) mkString("")
View ArticleAnswer by manish_s for How to generate a random alpha-numeric string?
You can use Apache library for this: RandomStringUtils RandomStringUtils.randomAlphanumeric(20).toUpperCase();
View ArticleAnswer by Bhavik Ambani for How to generate a random alpha-numeric string?
Best Random String Generator Method public class RandomStringGenerator{ private static int randomStringLength = 25 ; private static boolean allowSpecialCharacters = true ; private static String...
View ArticleAnswer by user unknown for How to generate a random alpha-numeric string?
A short and easy solution, but uses only lowercase and numerics: Random r = new java.util.Random (); String s = Long.toString (r.nextLong () & Long.MAX_VALUE, 36); The size is about 12 digits to...
View ArticleAnswer by cmpbah for How to generate a random alpha-numeric string?
import java.util.Random; public class passGen{ //Verison 1.0 private static final String dCase = "abcdefghijklmnopqrstuvwxyz"; private static final String uCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; private...
View ArticleAnswer by Jameskittu for How to generate a random alpha-numeric string?
import java.util.Date; import java.util.Random; public class RandomGenerator { private static Random random = new Random((new Date()).getTime()); public static String generateRandomString(int length) {...
View ArticleAnswer by michaelok for How to generate a random alpha-numeric string?
You mention "simple", but just in case anyone else is looking for something that meets more stringent security requirements, you might want to take a look at jpwgen. jpwgen is modeled after pwgen in...
View ArticleAnswer by Suganya for How to generate a random alpha-numeric string?
import java.util.*; import javax.swing.*; public class alphanumeric{ public static void main(String args[]){ String nval,lenval; int n,len; nval=JOptionPane.showInputDialog("Enter number of codes you...
View ArticleAnswer by dfa for How to generate a random alpha-numeric string?
using Dollar should be simple as: // "0123456789" + "ABCDE...Z" String validCharacters = $('0', '9').join() + $('A', 'Z').join(); String randomString(int length) { return...
View ArticleAnswer by anonymous for How to generate a random alpha-numeric string?
In one line: Long.toHexString(Double.doubleToLongBits(Math.random())); http://mynotes.wordpress.com/2009/07/23/java-generating-random-string/
View ArticleAnswer by Amar Nath Batta for How to generate a random alpha-numeric string?
I have developed an application to develop an auto generated alphanumberic string for my project. In this string, the first three chars are alphabetical and the next seven are integers. public class...
View ArticleAnswer by maxp for How to generate a random alpha-numeric string?
static final String AB = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; static SecureRandom rnd = new SecureRandom(); String randomString( int len ){ StringBuilder sb = new...
View ArticleAnswer by cmsherratt for How to generate a random alpha-numeric string?
If you're happy to use Apache classes, you could use org.apache.commons.text.RandomStringGenerator (commons-text). Example: RandomStringGenerator randomStringGenerator = new...
View ArticleAnswer by Todd for How to generate a random alpha-numeric string?
I found this solution that generates a random hex encoded string. The provided unit test seems to hold up to my primary use case. Although, it is slightly more complex than some of the other answers...
View ArticleAnswer by Steve McLeod for How to generate a random alpha-numeric string?
Java supplies a way of doing this directly. If you don't want the dashes, they are easy to strip out. Just use uuid.replace("-", "") import java.util.UUID; public class randomStringGenerator { public...
View ArticleAnswer by Apocalisp for How to generate a random alpha-numeric string?
Here it is in Java: import static java.lang.Math.round; import static java.lang.Math.random; import static java.lang.Math.pow; import static java.lang.Math.abs; import static java.lang.Math.min; import...
View ArticleAnswer by erickson for How to generate a random alpha-numeric string?
Algorithm To generate a random string, concatenate characters drawn randomly from the set of acceptable symbols until the string reaches the desired length. Implementation Here's some fairly simple and...
View ArticleHow to generate a random alpha-numeric string?
I've been looking for a simple Java algorithm to generate a pseudo-random alpha-numeric string. In my situation it would be used as a unique session/key identifier that would "likely" be unique over...
View ArticleAnswer by Riley Jones for How to generate a random alpha-numeric string?
public static String RandomAlphanum(int length) { String charstring = "abcdefghijklmnopqrstuvwxyz0123456789"; String randalphanum = ""; double randroll; String randchar; for (double i = 0; i <...
View ArticleAnswer by Chuong Tran for How to generate a random alpha-numeric string?
I'm using library from apache to generate alphanumeric stringimport org.apache.commons.lang3.RandomStringUtils;String keyLength = 20;RandomStringUtils.randomAlphanumeric(keylength);It's fast and simple!
View ArticleAnswer by Muxammed Gafarov for How to generate a random alpha-numeric string
public class Utils { private final Random RANDOM = new SecureRandom(); private final String ALPHABET = "0123456789QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm"; private String...
View ArticleAnswer by Mayank for How to generate a random alpha-numeric string
I am using a very simple solution using Java 8. Just customize it according to your needs....import java.security.SecureRandom;...//Generate a random String of length between 10 to 20.//Length is also...
View ArticleAnswer by F_SO_K for How to generate a random alpha-numeric string
You can do this in one line with no external libraries.int length = 12;String randomString = new Random().ints(48, 122).filter(i -> (i < 58 || i > 64) && (i < 91 || i >...
View ArticleAnswer by cwtuan for How to generate a random alpha-numeric string
Given the some characters (AllCharacters), you could pickup a character in the string randomly. Then use-for loop to get random charcter repeatedly.public class MyProgram { static String...
View ArticleAnswer by Chamith Malinda for How to generate a random alpha-numeric string
With come modifications to gurantee to have a number in the returned string. private static String generateRandomCouponMethod3(int lengthOfString){ final String alphabet =...
View Article