Now, I know that this is a simple question for MacOS, but when I compile a code with ‘arc4random % n’ in it, I just get an error log in Terminal saying:
main.m:9: error: ‘arc4random’ undeclared (first use in this function)
main.m:9: error: (Each undeclared identifier is reported only once
main.m:9: error: for each function it appears in.)
and I use:
gcc `gnustep-config --objc-flags` -lgnustep-base main.m -o main
to compile it
and here’s my code (if it helps) :
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int number, guess;
number = arc4random() % 101;
while (!guess == number) {
NSLog (@"Please guess a number between 1 and 100");
scanf ("%i", &guess);
if (guess < number) {
NSLog (@"Sorry, guessed too low!");
}
else if (guess > number) {
NSLog (@"Sorry, guessed too high!");
}
}
NSLog (@"You guessed correct!");
[pool drain];
return 0;
}
You may consider using
clanginstead ofgccUse
Also I’d use
arc4random_uniform(101)instead ofarc4random() % 101, since the former is bias free.