{"id":1368,"date":"2022-03-03T07:39:56","date_gmt":"2022-03-03T07:39:56","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1368"},"modified":"2022-03-03T07:39:56","modified_gmt":"2022-03-03T07:39:56","slug":"create-random-strings","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/03\/03\/create-random-strings\/","title":{"rendered":"Create random strings"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Java program to generate a random string<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.Random;\n\nclass Main {\n  public static void main(String&#91;] args) {\n\n    \/\/ create a string of all characters\n    String alphabet = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\";\n\n    \/\/ create random string builder\n    StringBuilder sb = new StringBuilder();\n\n    \/\/ create an object of Random class\n    Random random = new Random();\n\n    \/\/ specify length of random string\n    int length = 7;\n\n    for(int i = 0; i &lt; length; i++) {\n\n      \/\/ generate random index number\n      int index = random.nextInt(alphabet.length());\n\n      \/\/ get character specified by index\n      \/\/ from the string\n      char randomChar = alphabet.charAt(index);\n\n      \/\/ append the character to string builder\n      sb.append(randomChar);\n    }\n\n    String randomString = sb.toString();\n    System.out.println(\"Random String is: \" + randomString);\n\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Random String is: IIYOBRK<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have first created a string containing all the alphabets. Next, we have generated a random index number using the&nbsp;<code>nextInt()<\/code>&nbsp;method of the&nbsp;<code>Random<\/code>&nbsp;class.<\/p>\n\n\n\n<p>Using the random index number, we have generated the random character from the string alphabet. We then used the&nbsp;<code>StringBuilder<\/code>&nbsp;class to append all the characters together.<\/p>\n\n\n\n<p>If we want to change the random string into lower case, we can use the&nbsp;<code>toLowerCase()<\/code>&nbsp;method of the&nbsp;<code>String<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>randomString.toLowerCase()<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Java program to generate a random string Output In the above example, we have first created a string containing all the alphabets. Next, we have generated a random index number using the&nbsp;nextInt()&nbsp;method of the&nbsp;Random&nbsp;class. Using the random index number, we have generated the random character from the string alphabet. We then used the&nbsp;StringBuilder&nbsp;class to append [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[539],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1368"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1368"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1368\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1368"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1368"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1368"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}