Home >Java >javaTutorial >How to randomly generate non-repeating strings in java
Can be implemented using the random() function and valueOf() function.
The random() method is used to return a random number, the range of random numbers is 0.0 =< Math.random < 1.0.
valueOf(char c): method is used to return the string representation of the char parameter.
(Recommended video tutorial: java video)
The specific code is as follows:
public String getlinkNo() { String linkNo = ""; // 用字符数组的方式随机 String model = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; char[] m = model.toCharArray(); for (int j = 0; j < 6; j++) { char c = m[(int) (Math.random() * 36)]; // 保证六位随机数之间没有重复的 if (linkNo.contains(String.valueOf(c))) { j--; continue; } linkNo = linkNo + c; } return linkNo; }
Recommended tutorial: Getting started with java development
The above is the detailed content of How to randomly generate non-repeating strings in java. For more information, please follow other related articles on the PHP Chinese website!