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: 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:
|
<<: How are Vue components parsed and rendered?
>>: The main idea of dynamically setting routing permissions in Vue
Next, I will install Java+Tomcat on Centos7. Ther...
1. Use of Iframe tag <br />When it comes to ...
Uninstall tomcat9 1. Since the installation of To...
When learning mybatis, I encountered an error, th...
Table of contents background 1. The query conditi...
1. Download Navicat for MySQL 15 https://www.navi...
This article is just to commemorate those CSS que...
reason: MySQL 5.7.5 and up implements detection o...
There are many tags and elements in the HTML head ...
1. Error details Once when manually performing a ...
After the changes: innodb_buffer_pool_size=576M -...
location matching order 1. "=" prefix i...
1. Install mysql Run the following command to upd...
Database application is an indispensable part of ...
Table of contents Lazy Loading CSS styles: HTML p...