How to generate random uppercase characters in Objective-C

后端 未结 3 889
清歌不尽
清歌不尽 2021-02-11 06:45

How can I generate 20 Uppercase characters randomly in my Objective-C iOS app?

相关标签:
3条回答
  • 2021-02-11 07:36

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

    Will give you a random uppercase character.

    0 讨论(0)
  • 2021-02-11 07:40

    Store ALL uppercase Characters in array,And Generate Random numbers from 0-25,using rand() or arcg4rand(),then retrieve object ay random numbered index respectively.

    0 讨论(0)
  • 2021-02-11 07:45
    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];
    

    Hope that answer this is what you want. :-)

    Alberto

    0 讨论(0)
提交回复
热议问题