简体   繁体   English

生成指定长度的随机字符串,该字符串仅包含指定字符(在Java中)

[英]Generate a random string of specified length that contains only specified characters (in Java)

Does anybody know of a good way to generate a random String of specified length and characters in Java. 有人知道在Java中生成指定长度和字符的随机String的好方法吗?

For example 'length' could be 5 and 'possibleChars' could be 'a,b,c,1,2,3,!'. 例如,“长度”可以是5,“可能的字符”可以是“ a,b,c,1,2,3 ,!”。

So 所以

c!a1b is valid c!a1b有效

BUT

cba16 is not. cba16不是。

I could try to write something from scratch but I feel like this must be a common use case for things like generating passwords, generating coupon codes, etc... 我可以尝试从头开始编写一些东西,但我觉得这对于生成密码,生成优惠券代码等必定是一个常见用例。

Any ideas? 有任何想法吗?

The code to do this is pretty short. 执行此操作的代码很短。 Have a char[] or String with N legal chars, and, length times, pick a random number R between 0 and N-1, use R to pick a character to append to your generated String. 使用char []或具有N个合法字符的String,并在长度上选择一个介于0到N-1之间的随机数R,使用R来选择一个字符以附加到生成的String上。

You want something like this? 你想要这样的东西吗?

Random r=new Random();

char[] possibleChars="abc123!".toCharArray();
int length=5;

char[] newPassword=new char[length];

for (int i=0; i<length;i++)
    newPassword[i]=possibleChars[r.nextInt(possibleChars.length)];

System.out.println(new String(newPassword));

I could try to write something from scratch but I feel like this must be a common use case for things like generating passwords, generating coupon codes, etc... 我可以尝试从头开始编写一些东西,但我觉得这对于生成密码,生成优惠券代码等必定是一个常见用例。

It is not that common, and the detailed requirements are different each time. 这种情况并不常见,每次的详细要求都不相同。 Besides, the code is simple to the point of being trivial. 此外,代码很简单,以至于变得微不足道。 (Modulo the concerns below ... which are really about the requirements rather than the solution.) (模范下面的关注点……实际上是关于需求而非解决方案的。)

In short, it is quicker to write your own method than to go looking for an existing library method that does this. 简而言之,编写自己的方法比寻找现有的库方法要快。


When you use a scheme that involves random numbers, you need to be aware of the possibility that you will get collisions; 当您使用包含随机数的方案时,您需要意识到发生冲突的可能性。 ie that the method will generate the same random string on more than one occasion. 也就是说,该方法将多次生成相同的随机字符串。 You can mitigate this by using a longer string, but that only works to a certain point ... depending on your random number generator. 您可以通过使用更长的字符串来缓解这种情况,但这只能在一定程度上起作用……具体取决于您的随机数生成器。 (Typical random number generators are actually pseudo-random number generators, and produce a sequence of numbers that eventually cycle around. And even with a perfect random number generator there is a finite probability of repeats over a short sequence.) (典型的随机数生成器实际上是伪随机数生成器,并产生最终循环的数字序列。即使使用完美的随机数生成器,在短序列上重复的概率也有限。)

In fact, this is another reason why a "one size fits all" solution to your problem is not a good idea. 实际上,这是为什么“一刀切”的解决方案不是一个好主意的另一个原因。

If this is for real security, as opposed to homework or a programming exercise, then use SecureRandom, not Random. 如果这是为了真正的安全而不是作业或编程练习,请使用SecureRandom,而不是Random。

Read the Diceware website for a lot of very good ideas on the random generation of passwords and other things. 阅读Diceware网站,了解有关随机生成密码和其他内容的很多很好的想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM