You should use the arc4random_uniform()
function. It uses a superior algorithm to rand
. You don't even need to set a seed.
#include <stdlib.h>
// ...
// ...
int r = arc4random_uniform(74);
The arc4random
man 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))
Start with
NSUInteger prime = 31;
NSUInteger result = 1;
Then for every primitive you do
result = prime * result + var
For objects you use 0 for nil and otherwise their hashcode.
result = prime * result + [var hash];
For booleans you use two different values
result = prime * result + ((var)?1231:1237);
Explanation and Attribution
This is not tcurdt's work, and comments were asking for more explanation, so I believe an edit for attribution is fair.
This algorithm was popularized in the book "Effective Java", and the relevant chapter can currently be found online here. That book popularized the algorithm, which is now a default in a number of Java applications (including Eclipse). It derived, however, from an even older implementation which is variously attributed to Dan Bernstein or Chris Torek. That older algorithm originally floated around on Usenet, and certain attribution is difficult. For example, there is some interesting commentary in this Apache code (search for their names) that references the original source.
Bottom line is, this is a very old, simple hashing algorithm. It is not the most performant, and it is not even proven mathematically to be a "good" algorithm. But it is simple, and a lot of people have used it for a long time with good results, so it has a lot of historical support.
Best Answer
In Objective-C there's no standard protocol for comparison, like the
Comparable<T>
interface in Java, but some classes from the Foundation framework define acompare:
method.NSString
andNSNumber
are some examples.If you want to provide your own comparison method for a custom class, I suggest that you stick to conventions and provide a method with the following signature
NSComparisonResult
is an enum defined as followsNSOrderedAscending
means that the left operand is smaller than the right one (and from this you get the others).NOTE
It's worth stressing that Objective-C and the closely related Foundation Framework are built on conventions. They are not just an idiomatic way of writing, as sometimes the implementation relies on them. For instance if you instanciate a
NSSortDescriptor
without specifying a custom selector, the default implementation will callcompare:
on the objects. (hat-tip to David Ronnqvist for the example)Another notable example is the naming convention for methods. Instance methods starting with
init
are expected to return an object with a +1 retain count. This was such strong convention, that with the advent of ARC it has been formalized and built into the compiler.To wrap it up: conventions are always important. In Objective-C sometimes they are fundamental.