gpt4 book ai didi

ios - 生成任何恰好 18 位数字的随机 NSInteger

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:54:13 25 4
gpt4 key购买 nike

我试过这个:

NSInteger numberFinal = 100000000000000000 + ((float)arc4random() / UINT32_MAX) * (999999999999999999 - 100000000000000000);

但它返回零...我不想指定范围,但只想要任何 18 位数字...

最佳答案

根据您的要求,正如@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);

到此为止,无需解释,一切都很简单。

编辑:如果你真的想要一个 long long 值,你可以这样做:

long long eighteenDigitNumberLongLong = [eighteenDigitNumberString longLongValue];  

已编辑:为避免前导 0,初始字符串已使用非零数字启动,循环仅运行 17 次。

关于ios - 生成任何恰好 18 位数字的随机 NSInteger,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25567674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com