How can I generate 20 Uppercase characters randomly in my Objective-C iOS app?
Asked
Active
Viewed 720 times
2
-
how about use ascii http://stackoverflow.com/a/2832750/926460 – Timeless May 12 '12 at 05:03
3 Answers
11
char c = (char)('A' + arc4random_uniform(25))
Will give you a random uppercase character.

verdesmarald
- 11,646
- 2
- 44
- 60
-
Shouldn't this be **arc4random_uniform(26)** ? As the highest number of arc4random_uniform(25) is 24 (according to the [man page](https://developer.apple.com/library/mac/documentation/Darwin/Reference/Manpages/man3/arc4random_uniform.3.html)) and 'A' + 24 is 'Y'. – Gyfis Nov 27 '14 at 10:51
0
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.

Allamaprabhu
- 845
- 1
- 7
- 17
-
1Why would you bother will all the array manipulation when you can just do 'A' + your_random_number... – verdesmarald May 12 '12 at 05:12
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];
Hope that answer this is what you want. :-)
Alberto