{"id":3285,"date":"2017-03-31T15:12:32","date_gmt":"2017-03-31T09:42:32","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=3285"},"modified":"2018-10-25T17:34:52","modified_gmt":"2018-10-25T12:04:52","slug":"generate-random-alpha-numeric-string","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/generate-random-alpha-numeric-string\/","title":{"rendered":"How to generate a random alpha-numeric string?"},"content":{"rendered":"<ul>\n<li><strong>Generate a random alpha-numeric string<\/strong> whose length is the number of <a href=\"https:\/\/www.wikitechy.com\/tutorials\/java\/java-characters\" target=\"_blank\" rel=\"noopener\">characters<\/a> specified.<\/li>\n<li>Characters will be chosen from the set of alpha-numeric characters.<\/li>\n<li>Count is the length of random string to create.<\/li>\n<\/ul>\n[pastacode lang=\u201djava\u201d manual=\u201dprivate%20static%20final%20String%20ALPHA_NUMERIC_STRING%20%3D%20%22ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789%22%3B%0Apublic%20static%20String%20randomAlphaNumeric(int%20count)%20%7B%0AStringBuilder%20builder%20%3D%20new%20StringBuilder()%3B%0Awhile%20(count\u2013%20!%3D%200)%20%7B%0Aint%20character%20%3D%20(int)(Math.random()*ALPHA_NUMERIC_STRING.length())%3B%0Abuilder.append(ALPHA_NUMERIC_STRING.charAt(character))%3B%0A%7D%0Areturn%20builder.toString()%3B%0A%7D\u201d message=\u201dJava Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<ul>\n<li>To generate random alpha-numeric <a href=\"https:\/\/www.wikitechy.com\/technology\/java-programming-compare-two-strings-represented-linked-lists\/\" target=\"_blank\" rel=\"noopener\">String<\/a> objects<\/li>\n<li>Use <span style=\"color: #800000;\"><strong>random(int count, boolean letters, boolean numbers)<\/strong><\/span> method to create a random string whose length is the number of characters specified.<\/li>\n<li>Use <span style=\"color: #800000;\"><strong>randomAlphabetic(int count)<\/strong><\/span> method to create a random string whose length is the number of characters specified.<\/li>\n<li>Use<span style=\"color: #800000;\"> <strong>randomAscii(int count)<\/strong><\/span> method to create a random string whose length is the number of characters specified.<\/li>\n<li>Use <span style=\"color: #800000;\"><strong>random(int count, int start, int end, boolean letters, boolean numbers, char\u2026 chars)<\/strong><\/span> method to create a random string based on a variety of options, using default source of randomness,<\/li>\n<\/ul>\n[pastacode lang=\u201djava\u201d manual=\u201dimport%20org.apache.commons.lang3.RandomStringUtils%3B%0Apublic%20class%20RandomString%20%7B%0A%20%20%20%20public%20static%20void%20main(String%5B%5D%20args)%20%7B%0A%20%20%20%0A%20%20String%20string%20%3D%20RandomStringUtils.random(64%2C%20false%2C%20true)%3B%0A%20%20System.out.println(%22Random%200%20%3D%20%22%20%2B%20string)%3B%0A%20%20%20%20string%20%3D%20RandomStringUtils.randomAlphabetic(64)%3B%0A%20%20System.out.println(%22Random%201%20%3D%20%22%20%2B%20string)%3B\u201d message=\u201dJava Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n[pastacode lang=\u201djava\u201d manual=\u201d%0A%20%20string%20%3D%20RandomStringUtils.randomAscii(32)%3B%0A%20%20System.out.println(%22Random%202%20%3D%20%22%20%2B%20string)%3B%0A%20%20%20%0A%20%20string%20%3D%20RandomStringUtils.random(32%2C%200%2C%2020%2C%20true%2C%20true%2C%20%22bj81G5RDED3DC6142kasok%22.toCharArray())%3B%0A%20%20System.out.println(%22Random%203%20%3D%20%22%20%2B%20string)%3B%0A%20%20%20%20%7D%0A%7D\u201d message=\u201dJava Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h3 id=\"output\"><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/h3>\n<p>Random 0 = 0280748858014499019999655817886659056806824331462442367947839271<br \/>\nRandom 1 = BdODmKWjGtaKeFyYsNCbOPRzquNIIRMiEFPjqTSgbfMvMeZgNKihEdUdUXUniHUh<br \/>\nRandom 2 = `2G@|>\u2019\/JdI):yB9PD%S4sZp_@ e!S*\u2019<br \/>\nRandom 3 = 18Rk2DkkD3bsksE2RCDDRbC1bDCDa1D1<\/p>\n<h3 id=\"how-to-create-random-string-with-random-characters\"><span style=\"color: #800000;\"><strong>How to create random string with random characters?<\/strong> <\/span><\/h3>\n[pastacode lang=\u201djava\u201d manual=\u201dimport%20java.util.Random%3B%0A%20%0Apublic%20class%20MyStringRandomGen%20%7B%0A%20%0A%20%20%20%20private%20static%20final%20String%20CHAR_LIST%20%3D%20%0A%20%20%20%20%20%20%20%20%22abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890%22%3B%0A%20%20%20%20private%20static%20final%20int%20RANDOM_STRING_LENGTH%20%3D%2010%3B%0A%20%20%20%20%20%20%0A%20%20%20%20public%20String%20generateRandomString()%7B%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20StringBuffer%20randStr%20%3D%20new%20StringBuffer()%3B%0A%20%20%20%20%20%20%20%20for(int%20i%3D0%3B%20i%3CRANDOM_STRING_LENGTH%3B%20i%2B%2B)%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20int%20number%20%3D%20getRandomNumber()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20char%20ch%20%3D%20CHAR_LIST.charAt(number)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20randStr.append(ch)%3B%0A%20%7D%0A%20%20%20%20%20%20%20%20return%20randStr.toString()%3B%0A%20%20%20%20%7D\u201d message=\u201dJava Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[pastacode lang=\u201djava\u201d manual=\u201d%20%20%20%20private%20int%20getRandomNumber()%20%7B%0A%20%20%20%20%20%20%20%20int%20randomInt%20%3D%200%3B%0A%20%20%20%20%20%20%20%20Random%20randomGenerator%20%3D%20new%20Random()%3B%0A%20%20%20%20%20%20%20%20randomInt%20%3D%20randomGenerator.nextInt(CHAR_LIST.length())%3B%0A%20%20%20%20%20%20%20%20if%20(randomInt%20-%201%20%3D%3D%20-1)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20randomInt%3B%0A%20%20%20%20%20%20%20%20%7D%20else%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20randomInt%20-%201%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D\u201d message=\u201dJava Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n[pastacode lang=\u201djava\u201d manual=\u201d%20public%20static%20void%20main(String%20a%5B%5D)%7B%0A%20%20%20%20%20%20%20%20MyStringRandomGen%20msr%20%3D%20new%20MyStringRandomGen()%3B%0A%20%20%20%20%20%20%20%20System.out.println(msr.generateRandomString())%3B%0A%20%20%20%20%20%20%20%20System.out.println(msr.generateRandomString())%3B%0A%20%20%20%20%20%20%20%20System.out.println(msr.generateRandomString())%3B%0A%20%20%20%20%20%20%20%20System.out.println(msr.generateRandomString())%3B%0A%20%20%20%20%20%20%20%20System.out.println(msr.generateRandomString())%3B%0A%20%20%20%20%20%20%20%20System.out.println(msr.generateRandomString())%3B%0A%20%20%20%20%20%20%20%20System.out.println(msr.generateRandomString())%3B%0A%20%20%20%20%7D%0A%7D\u201d message=\u201dJava Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h3 id=\"output-2\"><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/h3>\n<p>UdX5PwalMI<br \/>\nqj62F7zuoR<br \/>\nndJUNiHSlh<br \/>\nYohYpF5MfA<br \/>\nmwX9UhI7Ci<br \/>\nbCn5bsk2em<br \/>\npzMXQDYaDx<\/p>\n<h3 id=\"generates-an-alphanumeric-string-based-on-specified-length\"><span style=\"color: #800080;\"><strong>Generates an alphanumeric string based on specified length :<\/strong> <\/span><\/h3>\n[pastacode lang=\u201djava\u201d manual=\u201dimport%20java.io.IOException%3B%0Aimport%20java.security.MessageDigest%3B%0Aimport%20java.security.NoSuchAlgorithmException%3B%0Aimport%20java.text.ParseException%3B%0Aimport%20java.text.SimpleDateFormat%3B%0Aimport%20java.util.ArrayList%3B%0Aimport%20java.util.Date%3B%0Aimport%20java.util.List%3B%0Aimport%20java.util.Random%3B%0Aimport%20sun.misc.BASE64Decoder%3B%0Aimport%20sun.misc.BASE64Encoder%3B%0A%0Apublic%20class%20StringUtil%20%7B%0A%0A%20%20private%20static%20Random%20random%20%3D%20new%20Random((new%20Date()).getTime())%3B\u201d message=\u201dJava Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n[pastacode lang=\u201djava\u201d manual=\u201d%20%0A%20%20%20%20public%20static%20String%20generateRandomString(int%20length)%20%7B%0A%20%20%20%20%20%20char%5B%5D%20values%20%3D%20%7B\u2019a\u2019%2C\u2019b\u2019%2C\u2019c\u2019%2C\u2019d\u2019%2C\u2019e\u2019%2C\u2019f\u2019%2C\u2019g\u2019%2C\u2019h\u2019%2C\u2019i\u2019%2C\u2019j\u2019%2C\u2019k\u2019%2C\u2019l\u2019%2C\u2019m\u2019%2C\u2019n\u2019%2C\u2019o\u2019%2C\u2019p\u2019%2C\u2019q\u2019%2C\u2019r\u2019%2C\u2019s\u2019%2C\u2019t\u2019%2C\u2019u\u2019%2C\u2019v\u2019%2C\u2019w\u2019%2C\u2019x\u2019%2C\u2019y\u2019%2C\u2019z\u2019%2C\u20190\u2019%2C\u20191\u2019%2C\u20192\u2019%2C\u20193\u2019%2C\u20194\u2019%2C\u20195\u2019%2C\u20196\u2019%2C\u20197\u2019%2C\u20198\u2019%2C\u20199\u2019%7D%3B%0A%20%20%20%20%20%20String%20out%20%3D%20%22%22%3B%0A%20%20%20%20%20%20for%20(int%20i%3D0%3Bi%3Clength%3Bi%2B%2B)%20%7B%0A%20%20%20%20%20%20%20%20%20%20int%20idx%3Drandom.nextInt(values.length)%3B%0A%20%20%20%20%20%20%20%20out%20%2B%3D%20values%5Bidx%5D%3B%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20return%20out%3B%0A%20%20%20%20%7D%0A%20%20%7D\u201d message=\u201dJava Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n","protected":false},"excerpt":{"rendered":"<p>JAVA &#8211; How to generate a random alpha-numeric string? Generate a random alpha numeric string whose length is the number of characters specified.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2139],"tags":[6135,6132,6129,6128,6130,6127,6131,6134,6133,6122,6120,6124,6126,6125,6121,6123],"class_list":["post-3285","post","type-post","status-publish","format-standard","hentry","category-java","tag-generate-a-random-alpha-numeric-string","tag-generate-random-alphanumeric-string","tag-generates-an-alphanumeric-string-based-on-specified-length","tag-generating-a-random-password-with-restrictions-in-java","tag-generating-random-alphanumeric-strings","tag-guava-random-string","tag-how-to-create-random-string-with-random-characters","tag-java-generate-random-string","tag-java-code-to-generate-a-random-alpha-numeric-string","tag-java-generate-random-string-from-array","tag-java-generate-random-string-of-specific-length","tag-java-generate-random-string-uuid","tag-java-random-string-from-list","tag-password-generator-in-java-source-code","tag-randomstringutils-java","tag-randomstringutils-java-example"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/3285","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=3285"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/3285\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=3285"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=3285"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=3285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}