with this code :
let rand : Int = Int(arc4random())
NSLog("rand = %d %i %@ \(rand)",rand,rand,String(rand))
I get :
rand = -1954814774 -1954814774 2340152522 2340152522
why all 4 values are not the same ?
with this code :
let rand : Int = Int(arc4random())
NSLog("rand = %d %i %@ \(rand)",rand,rand,String(rand))
I get :
rand = -1954814774 -1954814774 2340152522 2340152522
why all 4 values are not the same ?
Copyright © 2021 Jogjafile Inc.
arc4randomgenerates an unsigned 32bit int.Intis probably 64 bit on your machine so you get the same number and it doesn't overflow. But%iand%dare signed 32-bit format specifiers. See here and here. That's why you get a negative number whenarc4randomreturns a number greater than 2^32-1, akaInt32.max.For example, when 2340152522 is generated, you get
-1954814774in the%iposition because:On the other hand, converting an
InttoStringwon't change the number.Intis a signed 64 bit integer.