2

Objective-C iOS アプリで 20 個の大文字をランダムに生成するにはどうすればよいですか?

4

3 に答える 3

11

char c = (char)('A' + arc4random_uniform(25))

ランダムな大文字になります。

于 2012-05-12T05:11:35.923 に答える
0

すべての大文字を配列に格納し、rand() または arcg4rand() を使用して 0 ~ 25 の乱数を生成し、オブジェクトと乱数のインデックスをそれぞれ取得します。

于 2012-05-12T04:57:12.507 に答える
0
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];

これがあなたが望むものであることを願っています。:-)

アルベルト

于 2012-05-12T05:05:02.073 に答える