在 Cocoa 中生成随机字母数字字符串
我想调用一个方法,向其传递长度并让它生成一个随机字母数字字符串。
是否有任何实用程序库可能具有大量此类功能?
I want to call a method, pass it the length and have it generate a random alphanumeric string.
Are there any utility libraries out there that may have a bunch of these types of functions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(20)
Swift 中的替代解决方案
Alternative solution in Swift
这里修改了一些想法,并在完成 Swift 4.0
用法:
Modification of a few ideas here, and in done Swift 4.0
Usage:
如果您想要一个随机的 unicode 字符串,您可以创建随机字节,然后使用有效的字节。
要获得有效的 UTF-8 字符串,必须进行 NSString 与 NSData 之间的转换。
请注意,长度不一定是最终创建的 NSString 的长度。
If you want a random unicode string, you can create random bytes and then use the valid ones.
The conversion from NSString to NSData and back is necessary to get a valid UTF-8 string.
Be aware that length will not necessarily be the length of the the NSString created in the end.
我使用简单的
char[]
而不是字母表的NSString *
来完成此操作。我将其添加到 NSString 类别中。I did this using a simple
char[]
instead of anNSString *
for the alphabet. I added this to a NSString category.除了 Melvin 给出的良好答案之外,这里是我制作的一个函数(在 SWIFT 中!)来获取随机字符串:
这是调用
randomStringOfLength(10)
的测试结果:uXa0igA8wmAdding to good answer given by Melvin, here is a function I made (in SWIFT!) to get a random string:
Here is a test result from calling
randomStringOfLength(10)
: uXa0igA8wm生成给定长度的小写字母数字随机字符串:
Generates lowercase alphanumeric random string with given length:
调用方法:
方法:
结果:
Method to call:
Method:
Results:
适用于 Swift 3.0
for Swift 3.0
修改keithyip的答案:
Modification for keithyip's answer:
迅速
Swift
这里有一个不同的方法来解决这个问题。您可以在整数和字符之间进行转换,并生成要选择的动态字符列表,而不是使用准备好的字符串。它相当精简且快速,但代码较多。
当我进行随机字符生成时,我更喜欢直接使用整数并将其转换,而不是写出我想要从中绘制的字符列表。在顶部声明变量使其更加独立于系统,但此代码假设数字的值低于字母,并且大写字母的值低于小写字母。
Here's a different way to tackle it. Instead of using a prepared string of characters, you can cast between integers and characters, and generate a dynamic list of characters to select. It's pretty lean and fast, but has a bit more code.
When I'm doing random character generation, I prefer to work directly with integers and cast them over, instead of writing out the list of chars that I want to draw from. Declaring the variables at the top makes it more system independent, but this code assumes that numbers will have a lower value than letters, and that uppercase letters will have a lower value than lowercase letters.
这是一个快速而肮脏的实现。没有经过测试。
Here's a quick and dirty implementation. Hasn't been tested.
不完全符合您的要求,但仍然有用:
示例输出:
Not exactly what you ask, but still useful:
Sample output:
当然你可以把它缩短:
Surely you can make this shorter:
如果您愿意只使用十六进制字符,那么最简单的选择是生成 UUID:
示例输出:
16E3DF0B-87B3-4162-A1A1-E03DB2F59654
。如果您想要一个较小的随机字符串,那么您可以只获取前 8 个字符。
这是版本 4 UUID,这意味着第三组和第四组中的第一个字符不是随机的(它们始终是
4
以及8
、9
之一代码>、<代码>A或<代码>B)。字符串中的每个其他字符都是完全随机的,您可以在数百年内每秒生成数百万个 UUID,而不会产生相同 UUID 两次生成的风险。
If you're willing to limit yourself to hex characters only, then the simplest option is to generate a UUID:
Example output:
16E3DF0B-87B3-4162-A1A1-E03DB2F59654
.If you want a smaller random string then you can grab just the first 8 characters.
It's a version 4 UUID which means the first character in the 3rd and 4th group is not random (they will always be
4
and one of8
,9
,A
orB
).Every other character in the string is fully random and you can generate millions of UUIDs every second for hundreds of years without much risk of the same UUID being generated twice.
Jeff B 答案的类别版本。
NSString+Random.h
NSString+Random.m
A category version of Jeff B's answer.
NSString+Random.h
NSString+Random.m
您也可以只生成一个 UUID。虽然它们不是真正随机的,但它们复杂且独特,这使得它们在大多数用途中显得随机。生成一个字符串,然后获取等于传递长度的字符范围。
You could also just generate a UUID. While not truly random, they are complex and unique which makes them appear random for most uses. Generate one as a string and then take a range of characters equal to the passed length.