[英]How to generate a lot of unique random strings in Scala
I want to generate a lot of random strings that unique of each other and unique in MySQL with specify the number of generated like 10,000. 我想生成很多随机字符串,它们相互之间是唯一的,并且在MySQL中是唯一的,并指定了生成的数量,例如10,000。
I could make sure the strings are unique each other in every time generated strings, just check if the List[String]
contains the string in each time generate. 我可以确保每次生成的字符串中的字符串都是唯一的,只需检查
List[String]
是否在每次生成的字符串中都包含该字符串。
But I don't know how to make sure its unique in MySQL, I have been set the unique index in MySQL like ALTER TABLE Code ADD UNIQUE INDEX `codeUnique` (`code`);
但是我不知道如何确保它在MySQL中是唯一的,我已经在MySQL中设置了唯一索引,例如
ALTER TABLE Code ADD UNIQUE INDEX `codeUnique` (`code`);
But if the unique guarantee is when inserting data into MySQL, insert will failure. 但是,如果唯一的保证是将数据插入MySQL时,插入将失败。
And if check the insert failure in each time insert, generating will take a very very long time when the number of generated is large. 而且,如果在每次插入时都检查插入失败,那么当生成的数量很大时,生成将花费很长时间。
Use List.fill
and randomUUID
as @Sudhir notes: 使用
List.fill
和randomUUID
作为@Sudhir注释:
scala> List.fill(10000)(java.util.UUID.randomUUID.toString)
res0: List[String] = List(
3c789851-975c-499d-8b0b-5bc60dee7497,
9511fb41-a42e-47d8-a5f3-ba1a6f3847f2,
61263421-59de-4538-87c6-eb98ccf1e19d,
...)
You can double check they are unique by converting it to a set and checking the size: 您可以通过将其转换为集合并检查大小来再次检查它们的唯一性:
scala> List.fill(10000)(randomUUID.toString).toSet.size
res1: Int = 10000
You can simply use Java's java.util.UUID.randomUUID.toString
method to get unique String
. 您可以简单地使用Java的
java.util.UUID.randomUUID.toString
方法来获取唯一的String
。 See doc here . 在这里查看文档。 Chances of getting duplicate keys using this method is very low (I never got duplicate keys).
使用此方法获得重复密钥的机会非常低(我从未获得重复密钥)。 So you don't have to maintain a collection either.
因此,您也不必维护集合。
If you want to use your own random key generator and want to make sure it's unique, you should not go for List[String]
because List
can have duplicates and for checking if a key already exists, you need to iterate over each elements, which is a costly operation. 如果要使用自己的随机密钥生成器并确保其唯一性,则不要使用
List[String]
因为List
可能有重复项,并且为了检查密钥是否已存在,需要遍历每个元素,是一项昂贵的操作。 You should use Set[String]
instead and then use Set
's contains()
method to check if key is already present. 您应该改用
Set[String]
,然后使用Set
的contains()
方法检查密钥是否已经存在。
循环遍历10000个整数,取每个数字的MD5或SHA1。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.