Home  >  Article  >  Java  >  How to randomly generate non-repeating strings in java

How to randomly generate non-repeating strings in java

王林
王林Original
2020-05-20 16:38:114270browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn