2

我想要一个随机的字母表中所有字符的字符串。现在,我创建了一个包含 26 个字符的可变数组,使用 exchangeObjectAtIndex: 方法将它们打乱,然后将每个字符添加到我返回的字符串中。

必须有更好的方法来做到这一点。这是我的代码:

- (NSString *)shuffledAlphabet {
    NSMutableArray * shuffledAlphabet = [NSMutableArray arrayWithArray:@[@"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 (NSUInteger i = 0; i < [shuffledAlphabet count]; ++i) {
        // Select a random element between i and end of array to swap with.
        int nElements = [shuffledAlphabet count] - i;
        int n = (random() % nElements) + i;
        [shuffledAlphabet exchangeObjectAtIndex:i withObjectAtIndex:n];
    }

    NSString *string = [[NSString alloc] init];
    for (NSString *letter in shuffledAlphabet) {
        string = [NSString stringWithFormat:@"%@%@",string,letter];
    }

    return string;
}
4

4 回答 4

7

这是一个高效的Fisher-Yates shuffle,适用于您的用例:

- (NSString *)shuffledAlphabet {
    NSString *alphabet = @"ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    // Get the characters into a C array for efficient shuffling
    NSUInteger numberOfCharacters = [alphabet length];
    unichar *characters = calloc(numberOfCharacters, sizeof(unichar));
    [alphabet getCharacters:characters range:NSMakeRange(0, numberOfCharacters)];

    // Perform a Fisher-Yates shuffle
    for (NSUInteger i = 0; i < numberOfCharacters; ++i) {
        NSUInteger j = (arc4random_uniform(numberOfCharacters - i) + i);
        unichar c = characters[i];
        characters[i] = characters[j];
        characters[j] = c;
    }

    // Turn the result back into a string
    NSString *result = [NSString stringWithCharacters:characters length:numberOfCharacters];
    free(characters);
    return result;
}
于 2013-01-15T15:59:57.443 回答
2

这是执行正确混洗字母生成的更有效方法。

- (NSString *)shuffledAlphabet
{
    const NSUInteger length = 'Z' - 'A' + 1;
    unichar alphabet[length];
    alphabet[0] = 'A';

    for ( NSUInteger i = 1; i < length; i++ )
    {
        NSUInteger j = arc4random_uniform((uint32_t)i + 1);
        alphabet[i] = alphabet[j];
        alphabet[j] = 'A' + i;
    }
    return [NSString stringWithCharacters:alphabet length:length];
}

它使用 Fischer Yates shuffle 的“由内而外”版本,并通过生成伪随机数来避免模偏差arc4random_uniform。此外,它需要一次分配,因为所有排列都是在临时缓冲区中执行的。

于 2013-01-15T16:03:03.887 回答
1

在 Objective-C中生成随机数有帮助吗?*生成随机数 *除以26并取提醒 *索引数组[提醒]

于 2013-01-15T15:33:29.687 回答
0

您可以在构建字符串时从(剩余的)字母表中选择随机元素,而不是先对其进行改组:

NSMutableArray *alphabet = [NSMutableArray arrayWithObjects:@"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", nil];
NSMutableString *result = [NSMutableString string];
NSUInteger numberOfLetters = alphabet.count;
for (NSUInteger i = 0; i < numberOfLetters; i++) {
    int n = arc4random() % alphabet.count;
    [result appendString:[alphabet objectAtIndex:n]];
    [alphabet removeObjectAtIndex:n];
}
NSLog(@"%@", result);

这使得代码更短一些。另请注意,每次添加字母时使用NSMutableString比创建新字母更有效。NSString

于 2013-01-15T15:39:47.960 回答