Repeating random numbers


#1

When all our possessions were “lost” after a run it wasn’t an issue, but with possessions now being carried forward from one run to the next, you might notice that adding items results in the same sequence of serial numbers as in the prior run. That’s because the random number generator was never “seeded”. (Repeating is actually a feature, not a bug.)

To seed the random number generator (and thus generate a different set of serial numbers each time), add the following to Possession.m:

+ (void) initialize
{
    sranddev();
}

(Note the leading plus “+”; it’s a class method, not an instance method.)

The initialize message is sent to all Objective C classes so they can perform any necessary class-level initializations. As +randomPossession is a class method, +initialize is a good place to ensure the random number generator is seeded prior to any invocation of +randomPossession.