【发布时间】:2012-05-12 04:54:12
【问题描述】:
如何在我的 Objective-C iOS 应用中随机生成 20 个大写字符?
【问题讨论】:
-
用ascii怎么样stackoverflow.com/a/2832750/926460
标签: ios objective-c ios5
如何在我的 Objective-C iOS 应用中随机生成 20 个大写字符?
【问题讨论】:
标签: ios objective-c ios5
char c = (char)('A' + arc4random_uniform(25))
会给你一个随机的大写字符。
【讨论】:
将所有大写字符存储在数组中,并使用rand()或arcg4rand()生成0-25的随机数,然后分别检索对象的随机编号索引。
【讨论】:
NSMutableArray *a = [[NSMutableArray alloc] initWithObjects:@"A", @"B", @"C", @"D", @"E", nil]; // You can add more uppercases here.
int firstRandom = objectAtIndex:arc4random()%[a count];
NSString *s = [[NSString alloc] initWithFormat:@"%@", [a objectAtIndex:firstRandom]];
[a removeObjectAtIndex:firstRandom];
// Avoiding some uppercase repetitions. ;-)
for (int i = 0; i < [a count]; i++) {
int rndm = objectAtIndex:arc4random()%[a count];
s += [a objectAtIndex:rndm];
[a removeObjectAtIndex:rndm];
}
NSLog(@"%@", s);
[a release];
[s release];
希望这是您想要的答案。 :-)
阿尔伯托
【讨论】: