生成固定大小的随机字符串

时间:2018-09-02 09:55:36

标签: java

是否可以使用此代码生成大小为42个符号的随机字符串?

public static final String RANDOM_STRING = UUID.randomUUID().toString();

2 个答案:

答案 0 :(得分:0)

否,您不能创建超过36个字符,因为UUID的长度为128位。可以在这里找到更多信息。

java.util.UUID.randomUUID().toString() length

如果需要,将必须实现自己的功能以生成具有6 characters的随机字符串。为此,您可以使用Apache Commons Lang软件包。然后将其与UUID字符串连接。

答案 1 :(得分:0)

为了获得长度大于通常生成的36个字符的字符的UUID的真正随机性,可以从第二个生成的UUID(任意长度)中取出一块。可以全部在同一行代码中完成:

npm ERR! Error while executing:
npm ERR! /usr/bin/git ls-remote -h -t 
https://github.com/idxbroker/react-native-vcards.git
npm ERR!
npm ERR! remote: Invalid username or password.
npm ERR! fatal: Authentication failed for 
'https://github.com/idxbroker/react-native-vcards.git/'
npm ERR!
npm ERR! exited with error code: 128

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/dijish/.npm/_logs/2018-09-02T10_18_24_987Z- 
debug.log

请记住...这不是真正的UUID。

示例输出:

String RANDOM_STRING = UUID.randomUUID().toString() + "-" + 
                UUID.randomUUID().toString().substring(0, 5);

System.out.println("The UUID String is: " + RANDOM_STRING + 
        " and it is " + RANDOM_STRING.length() + " characters long.");