I have a problem with coding. I am trying to generate a Random number of 8 digits eg: 12345676 that show the number like this 12 34 56 76. A number of two digits, so a blank space, a number of two digits and other blank space... THANKS,
The code what I am using is:
NSMutableArray *numbers;
for(int i=0; i<3; i++){
float l_bound = 10;
float h_bound = 99;
float rndValue = (((float)arc4random()/0x100000000)*(h_bound-l_bound)+low_bound);
int intRndValue = (int)(rndValue + 0.5);
[numbers addObject: intRndValue]
;}
NSString *result = [numbers componentsJoinedByString:@" "];
At least you have to initialize the array
NSMutableArray *numbers = [NSMutableArray array];
and you can add only objects to the array rather than scalar types like int
or float
.
NSString *intRndValue = [NSString stringWithFormat:@"%.0f", (rndValue + 0.5)];
In Swift 2 you could write
var numbers = Array<String>()
let l_bound : UInt32 = 10
let h_bound : UInt32 = 99
for i in 0..<3 {
let rndValue = Int(arc4random_uniform(h_bound - l_bound) + l_bound)
numbers.append("\(rndValue)")
}
let result = numbers.joinWithSeparator(" ")