---
title: "What is the way to generate random alpha-numeric string in java?"  
description: "What is the way to generate random alpha-numeric string in java?"  
author: "Utpal Vishwas"  
published: 2023-07-21  
updated: 2023-07-22  
canonical: https://www.mindstick.com/forum/159231/what-is-the-way-to-generate-random-alpha-numeric-string-in-java  
category: "java"  
tags: ["java", "string"]  
reading_time: 3 minutes  

---

# What is the way to generate random alpha-numeric string in java?

How to [generate a random alpha-numeric string](https://www.mindstick.com/forum/23224/how-to-generate-a-random-alpha-numeric-string)?

## Replies

### Reply by Aryan Kumar

There are a few ways to generate a [random](https://www.mindstick.com/forum/33419/generating-random-numbers-in-objective-c) alphanumeric [string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp) in Java. Here are three methods:

**Method 1: Using the** `Random` **class**

The `Random` class provides a method called `nextInt()` that can be used to generate a random integer. To generate a random alphanumeric string, you can first generate a random integer between 0 and 99. Then, you can use the integer to index into a character array that contains all the alphanumeric characters. For example, the following code generates a random alphanumeric string of length 10:

Java

```plaintext
import java.util.Random;

public class RandomAlphanumericStringGenerator {

  public static void main(String[] args) {
    Random random = new Random();
    char[] characters = "abcdefghijklmnopqrstuvwxyz0123456789".toCharArray();
    String randomString = "";
    for (int i = 0; i < 10; i++) {
      randomString += characters[random.nextInt(99)];
    }
    System.out.println(randomString);
  }
}
```

**Method 2: Using the** `UUID` **class**

The `UUID` class provides a method called `randomUUID()` that generates a random UUID. A UUID is a 128-bit number that is guaranteed to be unique. To generate a random alphanumeric string, you can simply convert the UUID to a string. For example, the following code generates a random alphanumeric string of length 32:

Java

```plaintext
import java.util.UUID;

public class RandomAlphanumericStringGenerator {

  public static void main(String[] args) {
    UUID uuid = UUID.randomUUID();
    String randomString = uuid.toString();
    System.out.println(randomString);
  }
}
```

**Method 3: Using the** `Apache Commons Lang` **library**

The `Apache Commons Lang` library provides a class called `RandomStringUtils` that can be used to generate random strings. The `RandomStringUtils` class has a method called `randomAlphanumeric()` that can be used to generate a random alphanumeric string of the specified length. For example, the following code generates a random alphanumeric string of length 10 using the `Apache Commons Lang` library:

Java

```plaintext
import org.apache.commons.lang3.RandomStringUtils;

public class RandomAlphanumericStringGenerator {

  public static void main(String[] args) {
    String randomString = RandomStringUtils.randomAlphanumeric(10);
    System.out.println(randomString);
  }
}
```

Which method you use to generate a random alphanumeric string depends on your specific needs. If you need a random string that is guaranteed to be unique, then you should use the `UUID` class. If you need a random string that can be any length, then you should use the `RandomStringUtils` class. If you need a random string that is compatible with a specific character set, then you can use the `Random` class and specify the character set.


---

Original Source: https://www.mindstick.com/forum/159231/what-is-the-way-to-generate-random-alpha-numeric-string-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
