Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 生成随机字母表字符串的有效方法?_Ios_Objective C_Cocoa Touch_Nsstring_Nsmutablearray - Fatal编程技术网

Ios 生成随机字母表字符串的有效方法?

Ios 生成随机字母表字符串的有效方法?,ios,objective-c,cocoa-touch,nsstring,nsmutablearray,Ios,Objective C,Cocoa Touch,Nsstring,Nsmutablearray,我想要一个字母表中所有字符的随机字符串。现在,我创建了一个由26个字符组成的可变数组,使用exchangeObjectAtIndex:方法将它们洗牌,然后将每个字符添加到我返回的字符串中 必须有更好的方法来做到这一点。这是我的密码: - (NSString *)shuffledAlphabet { NSMutableArray * shuffledAlphabet = [NSMutableArray arrayWithArray:@[@"A",@"B",@"C",@"D",@"E",@"

我想要一个字母表中所有字符的随机字符串。现在,我创建了一个由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;
}
-(NSString*)shuffledAlphabet{
NSMutableArray*shuffledAlphabet=[NSMutableArray阵列: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”];
对于(整数i=0;i<[shuffledAlphabet计数];+i){
//在i和数组末尾之间选择一个随机元素进行交换。
int元素=[shuffledAlphabet计数]-i;
int n=(随机()%n元素)+i;
[shuffledAlphabet交换对象索引:i与对象索引:n];
}
NSString*string=[[NSString alloc]init];
for(SshuffledAlphabet中的NSString*字母){
字符串=[NSString stringWithFormat:@“%@%@”,字符串,字母];
}
返回字符串;
}
这有帮助吗? *生成随机数 *除以26并记下
*索引数组[提醒]

您可以在构建字符串时从(剩余)字母表中随机选取元素,而不是先将其洗牌:

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);
NSMutableArray*字母表=[NSMutableArray数组,其对象为:@“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”);
NSMutableString*结果=[NSMutableString];
NSU整数numberOfLetters=alphabet.count;
对于(整数i=0;i
这使得代码稍微短了一点。还要注意,每次添加字母时,使用
NSMutableString
比创建新的
NSString
效率更高。

下面是一个适合您的用例的高效方法:

- (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;
}
-(NSString*)shuffledAlphabet{
NSString*字母=@“ABCDEFGHIJKLMNOPQRSTUVWXYZ”;
//将字符放入一个C数组中,以实现高效的洗牌
NSU整数numberOfCharacters=[字母长度];
unichar*characters=calloc(numberOfCharacters,sizeof(unichar));
[alphabet getCharacters:字符范围:NSMakeRange(0,numberOfCharacters)];
//执行费舍尔·耶茨洗牌
for(整数i=0;i
这是执行正确无序字母表生成的更有效方法

- (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];
}
-(NSString*)shuffledAlphabet
{
常量整数长度='Z'-'A'+1;
unichar字母[长度];
字母表[0]=“A”;
对于(整数i=1;i

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

您想要随机字母串,还是想要随机排列字母表中的字母(即“无重复的随机序列”)?-你的问题的标题暗示了第一个,你的代码暗示了后一个。我非常喜欢字母的重复,或者只是一个混乱的字母表。当我写这个方法的时候,首先想到的是混乱的字母表。来吧,决定你想要什么。他们两个真的不一样,你说得对。我需要说清楚:我想要字母表中字母的排列。具有随机顺序且无重复字母的26个字符的字符串。使用排序算法对数组进行无序排列不是一个好主意。首先,不同的组合不具有相同的概率。其次,它的渐近复杂度是
O(log(n)*n)
,而一个有效的洗牌算法的复杂度是
O(n)
。你应该看看对的回复。谢谢你的链接。我已经删除了替代版本。这个答案中应该有链接。我不明白他们为什么不展示。