how to create random string in java?

The java.util.Random class is used to generate random numbers. Java Random class objects are thread safe. It provides several methods to generate random numbers of type integer, double, long, float etc.

Note: Random class objects are not suitable for security sensitive applications so it is better to use java.security.SecureRandom in these cases.

Example

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.w3schools;
import java.util.Random;
public class Test {
private static final String CHAR_LIST =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
private static final int RANDOM_STRING_LENGTH = 10;
public String randomString(){
StringBuffer randomString = new StringBuffer();
for(int i=0; i<random_string_length; i++){="" int="" number="getRandomNumber();" char="" ch="CHAR_LIST.charAt(number);" randomstring.append(ch);="" }="" return="" randomstring.tostring();="" private="" getrandomnumber()="" {="" randomint="0;" random="" randomgenerator="new" random();="" if="" (randomint="" -="" 1="=" -1)="" randomint;="" else="" 1;="" public="" static="" void="" main(string="" args[]){="" test="" test();="" for(int="" i="0;" <="" 10="" ;="" system.out.println(test.randomstring());="" pre="">
<h2>Output</h2>
<pre class="EnlighterJSRAW" data-enlighter-language="html">VSYAPagocY
eqQtFzFxLV
uItvdGTNtM
LWLeuabMLD
VQQJedejSf
dNJEyRjSRd
cYHYCfrePD
AdJyJmflbf
WDzYQfUvCH
pnvsVaINHO
</pre>
<h2>Java random class examples</h2>
<ul>
<li><a href="https://www.w3schools.blog/java-random-class-tutorial">Java Random class</a></li>
<li><a href="https://www.w3schools.blog/how-to-generate-random-numbers-in-a-given-range-in-java">Java generate random numbers in a given range</a></li>
<li><a href="https://www.w3schools.blog/how-to-generate-same-random-number-sequence-every-time">Java generate same random number sequence</a></li>
<li><a href="https://www.w3schools.blog/how-to-create-random-string-in-java">Java create random string</a></li>
<li><a href="https://www.w3schools.blog/how-to-create-random-alphanumeric-in-java">Java create random alphanumeric</a></li>
</ul>
<div class="w3sch-d294d3b98098ecd54df38d2ac58bf2b5 w3sch-after-content" id="w3sch-d294d3b98098ecd54df38d2ac58bf2b5"></div> </random_string_length;>
package com.w3schools; import java.util.Random; public class Test { private static final String CHAR_LIST = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; private static final int RANDOM_STRING_LENGTH = 10; public String randomString(){ StringBuffer randomString = new StringBuffer(); for(int i=0; i<random_string_length; i++){="" int="" number="getRandomNumber();" char="" ch="CHAR_LIST.charAt(number);" randomstring.append(ch);="" }="" return="" randomstring.tostring();="" private="" getrandomnumber()="" {="" randomint="0;" random="" randomgenerator="new" random();="" if="" (randomint="" -="" 1="=" -1)="" randomint;="" else="" 1;="" public="" static="" void="" main(string="" args[]){="" test="" test();="" for(int="" i="0;" <="" 10="" ;="" system.out.println(test.randomstring());="" pre=""> <h2>Output</h2> <pre class="EnlighterJSRAW" data-enlighter-language="html">VSYAPagocY eqQtFzFxLV uItvdGTNtM LWLeuabMLD VQQJedejSf dNJEyRjSRd cYHYCfrePD AdJyJmflbf WDzYQfUvCH pnvsVaINHO </pre> <h2>Java random class examples</h2> <ul> <li><a href="https://www.w3schools.blog/java-random-class-tutorial">Java Random class</a></li> <li><a href="https://www.w3schools.blog/how-to-generate-random-numbers-in-a-given-range-in-java">Java generate random numbers in a given range</a></li> <li><a href="https://www.w3schools.blog/how-to-generate-same-random-number-sequence-every-time">Java generate same random number sequence</a></li> <li><a href="https://www.w3schools.blog/how-to-create-random-string-in-java">Java create random string</a></li> <li><a href="https://www.w3schools.blog/how-to-create-random-alphanumeric-in-java">Java create random alphanumeric</a></li> </ul> <div class="w3sch-d294d3b98098ecd54df38d2ac58bf2b5 w3sch-after-content" id="w3sch-d294d3b98098ecd54df38d2ac58bf2b5"></div> </random_string_length;>
package com.w3schools;

import java.util.Random;

public class Test {
	private static final String CHAR_LIST = 
	        "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	private static final int RANDOM_STRING_LENGTH = 10;
	public String randomString(){
		StringBuffer randomString = new StringBuffer();
                for(int i=0; i

Output

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
VSYAPagocY
eqQtFzFxLV
uItvdGTNtM
LWLeuabMLD
VQQJedejSf
dNJEyRjSRd
cYHYCfrePD
AdJyJmflbf
WDzYQfUvCH
pnvsVaINHO
VSYAPagocY eqQtFzFxLV uItvdGTNtM LWLeuabMLD VQQJedejSf dNJEyRjSRd cYHYCfrePD AdJyJmflbf WDzYQfUvCH pnvsVaINHO
VSYAPagocY
eqQtFzFxLV
uItvdGTNtM
LWLeuabMLD
VQQJedejSf
dNJEyRjSRd
cYHYCfrePD
AdJyJmflbf
WDzYQfUvCH
pnvsVaINHO

Java random class examples