vendredi 3 mars 2017

How to generate random negative NSInteger

I'm trying to generate random negative NSInteger using the following code,

+ (NSInteger)getNegativeRandomNumber {
    uint32_t random = arc4random_uniform(2147483647); // where 2147483647 is the max 32-bit positive value returned by random function
    return (NSInteger)(-1 * random); // convert the positive into negative number
}

But occasionally this code gives me positive numbers. Do you see any issue with this code? Any help would be appreciated.

Thanks, Vasu




Aucun commentaire:

Enregistrer un commentaire