Java example code to generate random characters

Java example code to generate random characters

Sample code:

import java.util.Random;
import java.util.UUID;

public class Dept {

	/**
	 * Generate a random string uuid
	 */
	public static String getUUID() {
		return UUID.randomUUID().toString();
	}

	/**
	 * Generate a random string uuid and replace "-" with ""
	 */
	public static String getUUNUM() {
		return UUID.randomUUID().toString().replace("-", "");
	}

	/**
	 * Generate a random number with a specified number of digits * @param n The number of digits of the generated random number */
	public static String getRandom(int n) {
		Random r = new Random();
		Long b = Math.abs(r.nextLong());
		return b.toString().substring(0, n);
	}

	public static void main(String[] args) {
		System.out.println(getUUNUM());
		System.out.println(getUUID());
		System.out.println(getRandom(6));
	}

}

Running results:

insert image description here

Supplement: Three methods of randomly generating strings in Java

1. Each position of the generated string may be a letter or number in str. The package that needs to be imported is import java.util.Random;

//length The length of the string required by the user public static String getRandomString(int length){
   String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
   Random random=new Random();
   StringBuffer sb = new StringBuffer();
   for(int i=0;i<length;i++){
    int number = random.nextInt(62);
    sb.append(str.charAt(number));
   }
   return sb.toString();
 }

2. You can specify a position as az, AZ or 0-9, and the package to be imported is import java.util.Random;

//You can specify the value range of a certain position in the string public static String getRandomString2(int length){
  Random random=new Random();
  StringBuffer sb = new StringBuffer();
  for(int i=0;i<length;i++){
    int number=random.nextInt(3);
    long result=0;
    switch(number){
     case 0:
       result = Math.round(Math.random()*25+65);
       sb.append(String.valueOf((char)result));
       break;
     case 1:
       result = Math.round(Math.random()*25+97);
       sb.append(String.valueOf((char)result));
       break;
     case 2:   
       sb.append(String.valueOf(new Random().nextInt(10)));
       break;
    }


   }
   return sb.toString();
 }

3. There is a RandomStringUtils class under the org.apache.commons.lang package, which has a randomAlphanumeric(int length) function that can randomly generate a string of length.

String filename = RandomStringUtils.randomAlphanumeric(10);

This is the end of this article about Java random character generation sample code. For more relevant Java random character content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Java Basics - Given a random string, how many letters are there? How many numbers?
  • Java implements a UUID tool class that generates random string primary keys
  • Two methods of generating random strings in java
  • Java generates a random string array example
  • Java generates a 6-digit random string containing letters and numbers
  • Java randomly generates strings (character random generation class generates random character combinations)

<<:  How are Vue components parsed and rendered?

>>:  The main idea of ​​​​dynamically setting routing permissions in Vue

Recommend

How to check disk usage in Linux

1. Use the df command to view the overall disk us...

Analysis of two usages of the a tag in HTML post request

Two examples of the use of the a tag in HTML post...

How are Vue components parsed and rendered?

Preface This article will explain how Vue compone...

Native js drag and drop function to create a slider example code

Drag and drop is a common function in the front e...

HTML table mouse drag sorting function

Effect picture: 1. Import files <script src=&q...

How to configure pseudo-static and client-adaptive Nginx

The backend uses the thinkphp3.2.3 framework. If ...

How does the MySQL database implement the XA specification?

MySQL consistency log What happens to uncommitted...

Whitespace processing in HTML/CSS and how to preserve whitespace in the page

Whitespace rules in HTML In HTML, multiple spaces...

Vue uses canvas handwriting input to recognize Chinese

Effect picture: Preface: Recently, I was working ...

Introduction to the steps of deploying redis in docker container

Table of contents 1 redis configuration file 2 Do...

Detailed steps for implementing timeout status monitoring in Apache FlinkCEP

CEP - Complex Event Processing. The payment has n...

js tag syntax usage details

Table of contents 1. Introduction to label statem...

How to use Nginx to solve front-end cross-domain problems

Preface When developing static pages, such as Vue...

JavaScript to achieve stair rolling special effects (jQuery implementation)

I believe everyone has used JD. There is a very c...