0

i'm java programmer that 'must' move on to obj-C for a while,

i got some confuse when generating random alphanumeric code... here my javacode:

PS: i want to generate code like this :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;
}

then i create on 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);
}

but i got "HpNz" for result AC88pP13 insted that hve pattern String,string, numeric,numeric, lowescase string,numeric,numeric...

that case screw my life for 3 days...

4

2 回答 2

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),然后将其转换为字符串。

于 2013-11-09T08:53:05.373 回答
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:&currentChar 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;
}
于 2015-12-14T13:01:57.427 回答