user1998963
user1998963

Reputation: 11

Objective C - Random Character Generator

im looking for some help for a random character generator. At the moment i know how to create the generator using switch and char but i would like the code to go through the awnsers yes, no and maybe and then display one of them indefinitely.

any help would be appreciated :)

P.S im using objective C on Xcode

Upvotes: 1

Views: 1446

Answers (2)

Alex Reynolds
Alex Reynolds

Reputation: 96937

Digits 60-95 translate to letters A-Z in an ASCII mapping. Just generate a random number in that range, cast it to a char, and you're done. For example:

#include <time.h>
#include <stdlib.h>
#include <stdio.h>

srand(time(NULL));
int r = rand() % 35 + 60;
char c = (char) r;

fprintf(stdout, "%c", c);
NSLog(@"%c", c); 
/* etc. */

Upvotes: 1

Luke Walsh
Luke Walsh

Reputation: 281

If I understand your question you just want to grab one of three characters at random. Simply use int r = arc4random() % 2; to generate a random integer between 0 and 2. Then use a switch case to evaluate the three possibilities.

Upvotes: 1

Related Questions