Because % n will give the remainder, which will be between 0 and n inclusive.

By the way, when you take an integer random number generator modulo a number, the result is not completely random, though it's usually close enough for most purposes. If you want to make the built-in rand more random you can use this:

Theoretically speaking, that's actually no more random. Any way you do it you have something pseudorandom (and not even a good one, Mersenne Twister is much better & faster) and you won't get an even distribution (although for this small modulus it's only slightly off).

Isuppose that I could seed it with the time but since what I am doing is simple I don't think it's necessary. All that is happening is that an array of 52 instances of a class are being separated into two equal arrays of twenty six. Then the contents of one array is added back the the main array x number of random times between 1 and 3. This is done alternating through the two 26 length arrays untill all of the elements from one of the arrays have been used, then the rest are thrown in at the end. This is done a random amount of times. I was figuring around 8-12 times. This should randomize it up relativly well I would think. My description seems a bit hard to follow. I could post it in code if that would be easier.

if you wanted to have repeatable random numbers then why dont you just make a number array and call from that? Using a repeatable random number sequence seems kind of pointless, since depending on your FPU your array of random numbers may differ from comp to comp.

Well, it isn't pointless given that if you want to play back games, you need it and it is completely repeatable since internally, integers are used. The OP doesn't need it but it's definitely something to think about since a lot of games do.

Thanks for the code snipits that makes more sense in some ways I suppose. Also if anyone else is looking at this later on guess I should mention that it will be used in a texas holdem game so there are no wild cards or anything like that. I appreciate the help though, from everyone. It will be a joy helping/learning on these forums I'm sure.

So, here's a version using NSMutableArray. I've compressed it down a bit, dropping the bits where I couldn't see the merit. Note that required initialization is done in an init method (not a make method). The shuffleDeck algorithm, though clear enough, was rather lengthy so, here I'll just use shuffle.

NSArray can reduce the amount of code you need to write. The method makeObjectsPerformSelector is more like Eiffel than Java. As for speed, check it against your version, write a test case that creates and shuffles 100 decks. It is possible for shuffle to leave some cards where they were originally, but that should only occur randomly. You could use: exchangeObjectAtIndex ((unsigned)random())%26 withObjectAtIndex ((unsigned)random())%26+26; but I doubt that would be significantly different. Your test cases might also compare against an unshuffled deck to determine the likelyhood that cards are where they started.

And it wouldn't handle jokers, if you added a method to insert jokers, you would have 54 cards.

I couldn't see why you needed to support NSCopying, but this deck should work much like a real deck.

You could also have an initWithCard: (Class*) for creating specialized cards or initYuker to create a Yuker deck (fewer cards) which would require refactoring shuffle. The Yuker deck could be a subclass. Your base deck could also have methods to tell the cards which are wild or trump.

We can change the dealCard method to ensure that cards know they are trump if trump changes after the first card is dealt.

We didn't need a dealloc method before, because we didn't retain anything. Perhaps Deck will check with its Game delegate to determine if a card is trump.

Note: that the code for setting trump (such as by suite) is not shown. The code here only maintains cards no longer in the deck. A changeTrump: method would set up all the cards in the deck, then use the notification scheme to update cards not in the deck (in play or in flops).