0

I'm trying to make a card game. I'm using the following code to draw a random card

iCard=random() % 55;

but icard is always starts out with 28. Seems like it gives back the numbers in the same order.

is there a way to get a diffrent random number each time the function is first called????

Ted pottel
  • 6,869
  • 21
  • 75
  • 134
  • Sanford has a class on iOS development, the first project is a card game. It might help you out. http://www.stanford.edu/class/cs193p/cgi-bin/drupal/ – Joe Million Jun 08 '14 at 23:09
  • Also duplicate of http://stackoverflow.com/questions/1449754/iphone-random-function-gives-me-the-same-random-number-everytime – rmaddy Jun 08 '14 at 23:48

3 Answers3

3

random() and rand() uses a seed and it is always the same seed sequence.

Use arc4random() it uses no seed and is 100% random

Arbitur
  • 38,684
  • 22
  • 91
  • 128
0

Try with this code: int random = min + arc4random() % (max - min + 1);

Julian F. Weinert
  • 7,474
  • 7
  • 59
  • 107
0
-(IMCard*) drawRandomCard
{
    IMCard *randomCard = nil;

    if ([self.cards count]) {
        unsigned index = arc4random() % [self.cards count];
        randomCard = self.cards[index];
        [self.cards removeObjectAtIndex:index];
    }


    return randomCard;
 }
Joe Million
  • 147
  • 9