I tried this:
NSInteger numberFinal = 100000000000000000 + ((float)arc4random() / UINT32_MAX) * (999999999999999999 - 100000000000000000);
but it returns zero... I don't want to specify the range, but just want any number with 18 digits...
I tried this:
NSInteger numberFinal = 100000000000000000 + ((float)arc4random() / UINT32_MAX) * (999999999999999999 - 100000000000000000);
but it returns zero... I don't want to specify the range, but just want any number with 18 digits...
You're getting into unsigned long long territory...
As the maximum value of an NSInteger is NSIntegerMax, you cann't use NSInteger for your purpose:
Prior to OS X v10.5, NSNotFound was defined as 0x7fffffff. This is 2147483647 (decimal).
If you need "any number" with 18 digits (as @A-Live assumes), you can take NSFloat for example.
For your requirement, as @duDE mentioned you can't use a
NSInteger
to save 18 digit number, but there is a solution usingNSString
.There we go, no need to explain everything is straightforward.
EDITED: if you really want a long long value you can do so:
EDITED: To avoid the leading 0 the initial string has been initiated with a non-zero number and the loop is running only 17 times.
If you need strictly 18 digits it would be better to use this code:
You need the first condition because with the possibility of 0.1 you may receive 0 as the first digit, then your 18-digit integer would become 17-digit, with 0.01 possibility - 16-digit integer etc.
A 18 digit integer will require a long long type.
Create two 9 digit random numbers, multiple one by 10^9 and add to the other.
Output:
If the number must have a leading non zero digit: