Hi,
I made a simple program with a single button, that when pressed gives me a random number. I ran into some issues which several calls of NSLog highlight. Here is my code:
BARandom.h:
BARandom.m:
In the three calls of NSLog, the second number printed is the same as the first, even though it should be 1 greater. The fourth number is different from the rest. In addition, the first three numbers (being the same) only differ by several hundred or so at each press, where as the fourth differs a lot, is sometimes negative, and often repeats.
Any ideas on what is going on?
I made a simple program with a single button, that when pressed gives me a random number. I ran into some issues which several calls of NSLog highlight. Here is my code:
BARandom.h:
Code:
#import <Cocoa/Cocoa.h>
@interface BARandom : NSObject {
NSNumber* probability;
}
- (IBAction)button:(id)sender;
@end
BARandom.m:
Code:
#import "BARandom.h"
#include <math.h>
@implementation BARandom
- (IBAction)button:(id)sender {
NSNumber* theNumb;
theNumb=[[NSNumber numberWithFloat:rand()] retain];
NSLog(@"%d", [theNumb floatValue]);
NSLog(@"%d", ([theNumb floatValue]+1));
NSLog(@"%d and %d", [theNumb floatValue],[theNumb floatValue]);
}
@end
In the three calls of NSLog, the second number printed is the same as the first, even though it should be 1 greater. The fourth number is different from the rest. In addition, the first three numbers (being the same) only differ by several hundred or so at each press, where as the fourth differs a lot, is sometimes negative, and often repeats.
Any ideas on what is going on?