views:

401

answers:

2

Hi,

I have 52 records in DB, get those and store in an Array. Now i want to distribute them in to four arrays each array will have to 7 records only so only 28 records will be among 4 arrays and remaining will be in new temp array. Actually this is a card based game with four players, card will distribute like this: begin from first player- first card for first player, second for second player, third for third player and fourth for fourth player. This process will repeat while each player have 7 cards.

So how i distribute them so that same cards possibility become minimize. should i handle by Random number or any new... Please suggest

Thanks,

Aaryan

A: 

You should check out this answer about Shuffle Bag.

When you distribute your cards, you make a list of your 52 cards and do something like that:

listOfCards = { /* init, order not important */ }
cardsToDistribute = []

28.times do
    nextCard = listOfCards[random(0, listOfCards.size)]

    listOfCards.remove(nextCard) //side effect: decrement listOfCards.size by 1

    cardsToDistribute << nextCard
end

7.times do
   player1.cards << cardsToDistribute.removeFirst //side effect: decrement cardsToDistribute.size by 1
   player2.cards << cardsToDistribute.removeFirst
   player3.cards << cardsToDistribute.removeFirst
   player4.cards << cardsToDistribute.removeFirst
end

(Sorry I don't know Objective-C very well so this is Rubyist pseudo-code ;)

Jodi
+1  A: 

Here's a category for NSMutableArray to shuffle the array in place. It does a decent job on small arrays, but note that the random number function used, mrand48(), doesn't contain enough bits of randomness to produce all possible shuffles of a deck of cards, so there's some bias in the shuffles produced. If your game is purely for entertainment, this may be sufficient. If not, you can replace mrand48() with a better generator.

Don't forget to seed the random number generator at start up with srand48().

// --- NSMutableArray+Random.h ---
#import <Foundation/Foundation.h>

@interface NSMutableArray (Random)
  /// Shuffle a mutable array in place.  Uses a Fisher-Yates shuffle as described
  /// in http://en.wikipedia.org/wiki/Fisher-Yates_shuffle .
  - (void)shuffle;
@end

// --- NSMutableArray+Random.m
#import "NSMutableArray+Random.h"

/// Return a pseudo-random unsigned integer in the range 
/// [0, exclusiveUpperBound) using the mrand48() function.
/// Seed the random number state by calling srand48().
/// See http://developer.apple.com/iphone/library/documentation/System/Conceptual/ManPages_iPhoneOS/man3/rand48.3.html
static NSUInteger randomUIntegerFromZeroUpTo(NSUInteger exclusiveUpperBound) {
  NSUInteger const maxUInteger = 0xffffffff;
  NSUInteger largestMultipleOfMaxUInteger 
      = maxUInteger - (maxUInteger % exclusiveUpperBound);
  // discard random integers outside the range [0, largestMultipleOfMaxUnsignedInteger)
  // to eliminate modulo bias
  NSUInteger randomUInteger;
  do {
    randomUInteger = (NSUInteger) mrand48();
  } while (randomUInteger >= largestMultipleOfMaxUInteger);
  return randomUInteger % exclusiveUpperBound;
}

@implementation NSMutableArray (Random)
- (void)shuffle {
  for (NSUInteger unshuffled = self.count; unshuffled > 1; --unshuffled) {
    NSUInteger index1 = unshuffled - 1;
    NSUInteger index2 = randomUIntegerFromZeroUpTo(unshuffled);
    [self exchangeObjectAtIndex:index1 withObjectAtIndex:index2];
  }
}
@end
Don McCaughey