我是java程序员,“必须”继续使用obj-C一段时间,
在生成随机字母数字代码时我有些困惑...这里是我的javacode:
PS:我想生成如下代码:Gh12PU67,AC88pP13,Bk81gH89
private String generateCode(){
String code = "";
Random r = new Random();
char[] c = new char[]{'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
for(int i = 0; i<4; i++){
int uplow = r.nextInt(2);
String temp = ""+ c[r.nextInt(c.length)];
if(uplow==1)
code = code + temp.toUpperCase();
else
code = code + temp;
if((i+1)%2==0){
code += r.nextInt(10);
code += r.nextInt(10);
}
}
return code;
}
然后我在OBJ-C上创建
-(void)generateCode{
NSString *alphabet = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXZY0123456789";
NSMutableString *s = [NSMutableString stringWithCapacity:4];
for (NSUInteger i = 0U; i < 4; i++) {
u_int32_t r = arc4random() % [alphabet length];
unichar c = [alphabet characterAtIndex:r];
[s appendFormat:@"%C", c];
}
NSLog(@"s-->%@",s);
}
但我得到“HpNz”的结果AC88pP13 insted hve pattern String,string,numeric,numeric,lowescase string,numeric,numeric ...
那个案子让我的生活停了3天......
答案 0 :(得分:1)
你的Objective-C代码看起来不错,但是(正如@Wain在上面的评论中正确地说的那样), Java函数函数包含在2个字母后面插入2位数字的逻辑 没有在Objective-C方法中复制。
我会把这个逻辑略微模糊不清,并把它写成
- (void)generateCode
{
static NSString *letters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXZY";
static NSString *digits = @"0123456789";
NSMutableString *s = [NSMutableString stringWithCapacity:8];
for (NSUInteger i = 0; i < 2; i++) {
uint32_t r;
// Append 2 random letters:
r = arc4random_uniform((uint32_t)[letters length]);
[s appendFormat:@"%C", [letters characterAtIndex:r]];
r = arc4random_uniform((uint32_t)[letters length]);
[s appendFormat:@"%C", [letters characterAtIndex:r]];
// Append 2 random digits:
r = arc4random_uniform((uint32_t)[digits length]);
[s appendFormat:@"%C", [digits characterAtIndex:r]];
r = arc4random_uniform((uint32_t)[digits length]);
[s appendFormat:@"%C", [digits characterAtIndex:r]];
}
NSLog(@"s-->%@",s);
}
备注(来自手册页):
arc4random_uniform(length)
优先于arc4random() % length
,
因为当上限不是2的幂时,它避免了“模偏差”。
备注: Java代码code += r.nextInt(10);
的逐字翻译
到Objective-C将是
r = arc4random_uniform(10);
[s appendString:[@(r) stringValue]];
从随机数创建NSNumber
对象@(r)
,然后
将其转换为字符串。
答案 1 :(得分:1)
如果你想要一个安全的随机字符串,你应该使用这个代码:
#define ASCII_START_NUMERS 0x30
#define ASCII_END_NUMERS 0x39
#define ASCII_START_LETTERS_A 0x41
#define ASCII_END_LETTERS_Z 0x5A
#define ASCII_START_LETTERS_a 0x61
#define ASCII_END_LETTERS_z 0x5A
-(NSString *)getRandomString:(int)length {
NSMutableString *result = [[NSMutableString alloc]init];
while (result.length != length) {
NSMutableData* data = [NSMutableData dataWithLength:1];
SecRandomCopyBytes(kSecRandomDefault, 1, [data mutableBytes]);
Byte currentChar = 0;
[data getBytes:¤tChar length:1];
NSString *s = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if (currentChar > ASCII_START_NUMERS && currentChar < ASCII_END_NUMERS) { // 0 to 0
[result appendString:s];
continue;
}
if (currentChar > ASCII_START_LETTERS_A && currentChar < ASCII_END_LETTERS_Z) { // A to Z
[result appendString:s];
continue;
}
if (currentChar > ASCII_START_LETTERS_a && currentChar < ASCII_END_LETTERS_z) { // a to z
[result appendString:s];
continue;
}
}
return result;
}