Search code examples
iosobjective-carc4random

arc4random() gives negative Number in iOS


Sometime arc4random() gives negative number also in objective C.

My code is as follow:

Try 1:

long ii = arc4random();

Try 2:

int i = arc4random();

How can I only get positivite random number?

Thank you,


Solution

  • No, it's always positive as it returns an unsigned 32-bit integer (manpage):

    u_int32_t arc4random(void);
    

    You are treating it as a signed integer, which is incorrect.