简体   繁体   English

生成任意正好为18位数字的随机NSInteger

[英]Generate any random NSInteger with exactly 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... 但它返回零...我不想指定范围,而只想要任何18位数字...

For your requirement, as @duDE mentioned you can't use a NSInteger to save 18 digit number, but there is a solution using NSString . 根据您的要求,正如@duDE所述,您不能使用NSInteger保存18位数字,但是有一种使用NSString的解决方案。

NSString *eighteenDigitNumberString = [[NSNumber numberWithInt:1 + arc4random_uniform(9)] stringValue];

for (int i = 0; i < 17; i++) {

    eighteenDigitNumberString = [eighteenDigitNumberString stringByAppendingString:[[NSNumber numberWithInt:arc4random_uniform(10)] stringValue]];
}

NSLog(@"eighteenDigitNumberString : %@", eighteenDigitNumberString);

There we go, no need to explain everything is straightforward. 到了这里,无需解释一切都是简单的。

EDITED: if you really want a long long value you can do so: 编辑:如果您真的想要一个长long值,可以这样做:

long long eighteenDigitNumberLongLong = [eighteenDigitNumberString longLongValue];  

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. 编辑:为了避免前导0,初始字符串已使用非零数字启动,并且循环仅运行17次。

As the maximum value of an NSInteger is NSIntegerMax , you cann't use NSInteger for your purpose: 由于NSInteger的最大值是NSIntegerMax ,因此不能出于目的使用NSInteger:

enum {
   NSNotFound = NSIntegerMax
};

Prior to OS X v10.5, NSNotFound was defined as 0x7fffffff . 在OS X v10.5之前,NSNotFound定义为0x7fffffff This is 2147483647 (decimal). 这是2147483647 (十进制)。

If you need "any number" with 18 digits (as @A-Live assumes), you can take NSFloat for example. 如果需要18位数字的“任何数字”(如@ A-Live假定的那样),则可以以NSFloat为例。

A 18 digit integer will require a long long type. 18位整数将需要long long类型。

Create two 9 digit random numbers, multiple one by 10^9 and add to the other. 创建两个9位数的随机数,一个乘以10 ^ 9,然后相加。

const u_int32_t digits9 = 1000000000;
u_int32_t ms = arc4random_uniform(digits9);
u_int32_t ls = arc4random_uniform(digits9);
unsigned long long random18 = ((unsigned long long)ms * digits9) + ls;

NSLog(@"Example random18: %018llu", random18);

Output: 输出:

Example random18: 501895974656079554 示例random18:501895974656079554

If the number must have a leading non zero digit: 如果数字必须以非零开头的数字:

const u_int32_t digits81 = 100000000;
const u_int32_t digits89 = 900000000;
const u_int32_t digits9 = 1000000000;

u_int32_t ms = arc4random_uniform(digits89) + digits81;
u_int32_t ls = arc4random_uniform(digits9);
unsigned long long random18 = ((unsigned long long)ms * digits9) + ls;

If you need strictly 18 digits it would be better to use this code: 如果您只需要18位数字,则最好使用以下代码:

NSString *stringNumber = [NSString string];

for (int i = 0; i < 18; i++) {
    if (i == 0) {   
        stringNumber = [stringNumber stringByAppendingString:[NSString stringWithFormat:@"%@", @(arc4random_uniform(9) + 1)]];
    } else {
        stringNumber = [stringNumber stringByAppendingString:[NSString stringWithFormat:@"%@", @(arc4random_uniform(10))]];
    }
}

long long value = stringNumber.longLongValue;

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. 您需要第一个条件,因为可能性为0.1时您可能会收到0作为第一位数字,那么您的18位整数将变为17位数字,可能性为0.01-16位整数等。

You're getting into unsigned long long territory... 您正在进入无符号的长领域...

#define ARC4RANDOM_MAX      0x100000000

float val = ((double)arc4random() / ARC4RANDOM_MAX);
unsigned long long numberToAdd = val * (900000000000000000-1);
unsigned long long numberFinal = 100000000000000000 + numberToAdd;

NSLog( @"value = %llu", numberFinal);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM