0

I am generating numbers from 1-52 (to deal out random playing cards), however I want to make sure that none of them are the same. In total a max of 17 cards will be dealt out, and they must be different. This is not a duplicate, as no question answers how to check for repetitions in random numbers of more that 2 different values.

Here is a shortened version of the code for convenience:

    let randomPlayerCard1 = Int(arc4random() % 52) // Generates random numbers
    let randomPlayerCard2 = Int(arc4random() % 52)
    let randomCentreCard1 = Int(arc4random() % 52)
    let randomCentreCard2 = Int(arc4random() % 52)
    let randomCentreCard3 = Int(arc4random() % 52)
    let randomCentreCard4 = Int(arc4random() % 52)
    let randomCentreCard5 = Int(arc4random() % 52)

    while randomPlayerCard1 == randomPlayerCard2  { /* I don't want to create "while" 
statement for every combination of 2 of the eventual 17 random values being equal*/

    }
    playerCard1.image = mapping[randomPlayerCard1]
    playerCard2.image = mapping[randomPlayerCard2]

I also tried something like:

while randomPlayerCard1 == randomPlayerCard2 == randomCentreCard5 == randomCentreCard3

But your not allowed to do that, as I expected.

Thank you for any help!

RufusV
  • 408
  • 3
  • 17
  • I would make a `deck` array, initialized to all 52 cards, and I would draw random cards from it by generating a random number, and removing that card. – Alexander Jul 17 '16 at 03:22
  • I already wrote this answer, and the question got closed =/ http://pastebin.com/J8td637w – Alexander Jul 17 '16 at 03:29
  • You claim this is not a duplicate: "as no question answers how to check for repetitions in random numbers of more that 2 different values". Let me start by saying: _That is not the solution you are looking for._ By removing 'duplicates' in a random number sequence you would ***break*** the distribution properties of your pseudo-random sequence. So the ***correct*** solution is to use a random sequence to shuffle a full deck (or array). Refer to the linked duplicate and check out the many related questions for some enlightening reading. – Disillusioned Jul 17 '16 at 04:16

0 Answers0