gpt4 book ai didi

string - 生成一串随机字符 cocoa ?

转载 作者:行者123 更新时间:2023-12-03 16:33:04 25 4
gpt4 key购买 nike

我知道如何生成随机数,但我真正需要的是一串随机字符。这是我到目前为止所拥有的:

NSString *letters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789?%$";

char generated;
generated = //how do i define this?

NSLog(@"generated =%c", generated);
[textField setStringValue:generated];

最佳答案

-(NSString*)generateRandomString:(int)num {
NSMutableString* string = [NSMutableString stringWithCapacity:num];
for (int i = 0; i < num; i++) {
[string appendFormat:@"%C", (unichar)('a' + arc4random_uniform(25))];
}
return string;
}

然后对于 5 个字母的字符串,这样调用:

NSString* string = [self generateRandomString:5];

关于string - 生成一串随机字符 cocoa ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6543295/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com