I\'m a Java head mainly, and I want a way to generate a pseudo-random number between 0 and 74. In Java I would use the method:
Random.nextInt(74)
I thought I could add a method 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 a macro as
#define RAND_FROM_TO(min, max) (min + arc4random_uniform(max - min + 1))
E.g.
NSInteger myInteger = RAND_FROM_TO(0, 74) // 0, 1, 2,..., 73, 74
Note: Only for iOS 4.3/OS X v10.7 (Lion) and later
Same as C, you would do
#include <time.h>
#include <stdlib.h>
...
srand(time(NULL));
int r = rand() % 74;
(assuming you meant including 0 but excluding 74, which is what your Java example does)
Edit: Feel free to substitute random()
or arc4random()
for rand()
(which is, as others have pointed out, quite sucky).
There are some great, articulate answers already, but the question asks for a random number between 0 and 74. Use:
arc4random_uniform(75)
According to the manual page for rand(3), the rand family of functions have been obsoleted by random(3). This is due to the fact that the lower 12 bits of rand() go through a cyclic pattern. To get a random number, just seed the generator by calling srandom() with an unsigned seed, and then call random(). So, the equivalent of the code above would be
#import <stdlib.h>
#import <time.h>
srandom(time(NULL));
random() % 74;
You'll only need to call srandom() once in your program unless you want to change your seed. Although you said you didn't want a discussion of truly random values, rand() is a pretty bad random number generator, and random() still suffers from modulo bias, as it will generate a number between 0 and RAND_MAX. So, e.g. if RAND_MAX is 3, and you want a random number between 0 and 2, you're twice as likely to get a 0 than a 1 or a 2.
For game dev use random() to generate randoms. Probably at least 5x faster than using arc4random(). Modulo bias is not an issue, especially for games, when generating randoms using the full range of random(). Be sure to seed first. Call srandomdev() in AppDelegate. Here's some helper functions:
static inline int random_range(int low, int high){ return (random()%(high-low+1))+low;}
static inline CGFloat frandom(){ return (CGFloat)random()/UINT32_C(0x7FFFFFFF);}
static inline CGFloat frandom_range(CGFloat low, CGFloat high){ return (high-low)*frandom()+low;}
As of iOS 9 and OS X 10.11, you can use the new GameplayKit classes to generate random numbers in a variety of ways.
You have four source types to choose from: a general random source (unnamed, down to the system to choose what it does), linear congruential, ARC4 and Mersenne Twister. These can generate random ints, floats and bools.
At the simplest level, you can generate a random number from the system's built-in random source like this:
NSInteger rand = [[GKRandomSource sharedRandom] nextInt];
That generates a number between -2,147,483,648 and 2,147,483,647. If you want a number between 0 and an upper bound (exclusive) you'd use this:
NSInteger rand6 = [[GKRandomSource sharedRandom] nextIntWithUpperBound:6];
GameplayKit has some convenience constructors built in to work with dice. For example, you can roll a six-sided die like this:
GKRandomDistribution *d6 = [GKRandomDistribution d6];
[d6 nextInt];
Plus you can shape the random distribution by using things like GKShuffledDistribution
.