Generating Random Numbers in Objective-C

I'm mainly a Java header, and I want a way to generate pseudo-random numbers between 0 and 74.I use this method in Java:


I'm not interested in discussions about seeds or real randomness, just how you accomplish the same task in Objective-C.When I search for Google, I seem to have a lot of different and conflicting information.

#1st floor

It is best to use arc4random_uniform.However, this does not apply to iOS 4.3.Fortunately, iOS will bind this symbol at runtime instead of at compile time (so do not use the #if preprocessor directive to check if it is available).

The best way to determine if arc4random_uniform is available is to do the following:

#include <stdlib.h>

int r = 0;
if (arc4random_uniform != NULL)
    r = arc4random_uniform (74);
    r = (arc4random() % 74);

#2nd floor

Like C, you would do this

#include <time.h>
#include <stdlib.h>
int r = rand() % 74;

(Assuming you mean include 0 but not 74, this is what your Java example does)

Edit: rand() random() or arc4random() rand() is a bad replacement, as others have pointed out.

#3rd floor

rand series functions have been randomly phased out (3) according to the rand (3) manual page.This is because the lower 12 bits of rand () undergo a cyclic pattern.To get a random number, simply call srandom() with an unsigned seed to generate the generator, then call random().So, equivalent to the code above

#import <stdlib.h>
#import <time.h>

random() % 74;

You only need to call srandom() once in the program unless you want to change the seed.Although you say you don't want to talk about real random numbers, rand () is a very bad random number generator, and random () still has a modular bias because it produces numbers between 0 and RAND_MAX.So, for example, if RAND_MAX is 3 and you want a random number between 0 and 2, you are twice as likely to get 0 as 1 or 2.

#4th floor

You should use the arc4random_uniform() function.It uses superior algorithms to rand.You don't even need to seed.

#include <stdlib.h>
// ...
// ...
int r = arc4random_uniform(74);

arc4random manual page:

NAME arc4random, arc4random_stir, arc4random_addrandom -- arc4 random number generator LIBRARY Standard C Library (libc, -lc) SYNOPSIS #include <stdlib.h> u_int32_t arc4random(void); void arc4random_stir(void); void arc4random_addrandom(unsigned char *dat, int datlen); DESCRIPTION The arc4random() function uses the key stream generator employed by the arc4 cipher, which uses 8*8 8 bit S-Boxes. The S-Boxes can be in about (2**1700) states. The arc4random() function returns pseudo- random numbers in the range of 0 to (2**32)-1, and therefore has twice the range of rand(3) and random(3). The arc4random_stir() function reads data from /dev/urandom and uses it to permute the S-Boxes via arc4random_addrandom(). There is no need to call arc4random_stir() before using arc4random(), since arc4random() automatically initializes itself. EXAMPLES The following produces a drop-in replacement for the traditional rand() and random() functions using arc4random(): #define foo4random() (arc4random() % ((unsigned)RAND_MAX + 1))

#5th floor

I think I can add a method that I use in many projects.

- (NSInteger)randomValueBetween:(NSInteger)min and:(NSInteger)max {
    return (NSInteger)(min + arc4random_uniform(max - min + 1));

If I end up using it in many files, I usually declare macros as

#define RAND_FROM_TO(min, max) (min + arc4random_uniform(max - min + 1))

for example

NSInteger myInteger = RAND_FROM_TO(0, 74) // 0, 1, 2,..., 73, 74

Note: Only applicable to iOS 4.3 / OS X v10.7 (Lion) and later versions

Tags: Java iOS Google OS X

Posted on Sat, 21 Dec 2019 19:49:58 -0800 by coreysnyder04