Swift2 如何在Xcode 7和Swift 2中创建特殊格式的随机数

Swift2 如何在Xcode 7和Swift 2中创建特殊格式的随机数,swift2,arc4random,Swift2,Arc4random,我在编码方面有问题。我试图生成一个8位数的随机数,例如:12345676,它显示的数字如下12 34 56 76。一个两位数的数字,所以一个空格,一个两位数的数字和其他空格。。。谢谢 我使用的代码是: NSMutableArray *numbers; for(int i=0; i<3; i++){ float l_bound = 10; float h_bound = 99; float rndValue = (((float)arc4random()/0x10000000

我在编码方面有问题。我试图生成一个8位数的随机数,例如:12345676,它显示的数字如下12 34 56 76。一个两位数的数字,所以一个空格,一个两位数的数字和其他空格。。。谢谢

我使用的代码是:

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:@" "];
NSMutableArray*数字;

对于(int i=0;i至少您必须初始化数组

NSMutableArray *numbers = [NSMutableArray array];
您可以只向数组中添加对象,而不是像
int
float
这样的标量类型

NSString *intRndValue = [NSString stringWithFormat:@"%.0f", (rndValue + 0.5)];
用Swift 2你可以写

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(" ")
var numbers=Array()
设l_界:UInt32=10
设h_界:UInt32=99
因为我在0。。