Possessions are not random


#1

After working my way through Chapter 10, I’ve just now realized that my Random Possessions are no longer random. Every time I run the application, I get the exact same batch of results, same name, same value in dollars, etc.

I have found that if I change the items in the randomPossession method, for instance change “spork” to “cheese”, the name changes the next time I run the application, but still in the same spot, same order, and with all other values the same.

Any thoughts?

Here is my randomPossession, but it should be the same as in the book:

[code]+(id) randomPossession
{
NSArray *randomAdjectiveList = [NSArray arrayWithObjects:@“Fluffy”,
@“Rusty”,
@“Shiny”, nil];

NSArray *randomNounList = [NSArray arrayWithObjects:@"Bear",
                                                    @"Spark",
                                                    @"Mac", nil];

int adjectiveIndex = rand() % [randomAdjectiveList count];
int nounIndex = rand() % [randomNounList count];

NSString *randomName = [NSString stringWithFormat:@"%@ %@",
                        [randomAdjectiveList objectAtIndex:adjectiveIndex],
                        [randomNounList objectAtIndex:nounIndex]];

int randomValue = rand() % 100;

NSString *randomSerialNumber = [NSString stringWithFormat:@"%c%c%c%c%c",
                                '0' + rand() % 10,
                                'A' + rand() % 26,
                                '0' + rand() % 10,
                                'A' + rand() % 26,
                                '0' + rand() % 10];

Possession *newPossession = [[self alloc] initWithPossessionName:randomName
                                                  valueInDollars:randomValue
                                                    serialNumber:randomSerialNumber];
return newPossession;

}[/code]

Thanks!


#2

Joe Conway explains this, along with how to more truly randomize this function, in this post: http://forums.bignerdranch.com/viewtopic.php?f=77&t=2914

-Doug