About Me

My photo
Drum & Bass Producer, Software Developer, Love my Cats

iPhone : How to generate random emails in Objective-C

I'm building an app that manages email contacts. I wanted to have mock data. So I wrote this code to generate random strings and emails for me:


- (NSString*) getRandomEmail
{
    NSString *answer = [NSString stringWithFormat:@"%@@%@.com", [self getRandomString], [self getRandomString]];
    return answer;
}

- (NSString*) getRandomString
{
    NSString *letters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    int len = [self getRandomIntBetweenBottomInt:1 andTopInt:7];
    NSMutableString *randomString = [NSMutableString stringWithCapacity: len];
    for (int i=0; i < len; i++) {
        [randomString appendFormat: @"%C", [letters characterAtIndex: arc4random() % [letters length]]];
    }
    return randomString;
}

- (int) getRandomIntBetweenBottomInt:(int)bottom andTopInt:(int)top
{
    int randomNumber = (arc4random() % top) + bottom;
    return randomNumber;
}

and this is the result: