I have a random number generator method in iOS that I am currently using. While it is working fine, I would like to modify it such that in its output, it ALWAYS includes at least one number. How would I do this? Here is the random number method that I am using:
- (NSString *)generateRandString {
NSString *alphabet = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXZY0123456789";
NSMutableString *s = [NSMutableString stringWithCapacity:5];
for (NSUInteger i = 0U; i < 5; i++) {
u_int32_t r = arc4random() % [alphabet length];
unichar c = [alphabet characterAtIndex:r];
[s appendFormat:@"%C", c];
}
return s;
}
Thanks in advance to all who reply.
This is my edited and checked solution:
I’ve also implemented the solution suggested by Dan Dyer (and it’s obviously better than mine). My implementation is probably not perfect ( i use the same functionality few times instead of creating a separate function for it, but i believe it’s not difficult to implement for you) but it works exactly as Dan has proposed.